Skip to content

Trigger scripts

Trigger scripts are designed to pull data from an external source and return all of the new items since the last run, without resorting to external webhooks. A trigger script is intended to be used as a scheduled poll with schedules and states (rich objects in JSON, persistent from one run to another) in order to compare the execution to the previous one and process each new item in a for loop. If there are no new items, the flow will be skipped.

For more complex objects, or when you need structured relational data instead of simple state tracking, consider using data tables, which provide a workspace-scoped SQL database for storing and querying data.

Think of this as someone who checks the mailbox every day. If there is a new letter, they will continue to process it — open and read it — and if there is no new letter, they won’t do anything. The key part is that opened letters are not placed back in the mailbox. In Orvanta, a trigger script has the job of keeping track of what’s processed and what’s not.

Flows can be scheduled through the Flow UI using a CRON expression and then activating the schedule.

Examples of trigger scripts include:

  • Trigger every time a new item text on HackerNews matches at least one mention.
  • Notify of new GitHub repo stars.
  • Check new uploaded files on Google Drive.

The following TypeScript example checks for new documents in a MongoDB collection:

import { getState, type Resource, setState } from 'npm:orvanta-client';
import { MongoClient, ObjectId } from 'https://deno.land/x/atlas_sdk/mod.ts';
type MongodbRest = {
endpoint: string;
api_key: string;
};
export async function main(
auth: MongodbRest,
data_source: string,
database: string,
collection: string) {
const client = new MongoClient({
endpoint: auth.endpoint,
dataSource: data_source,
auth: { apiKey: auth.api_key }
});
const documents = client.database(database).collection(collection);
const lastCheck = (await getState()) || 0;
await setState(Date.now() / 1000);
const id = ObjectId.createFromTime(lastCheck);
return await documents.find({ _id: { $gt: id } });
}

This script uses Orvanta’s built-in state functions to store and update timestamps across runs.