> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getoperate.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Triggers

> 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 with [schedules](/workflows/events/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](/workflows/events/loops).  If there are no new items, the flow will be skipped.

By default, adding a trigger will set the schedule to 15 minutes.

<Note>
  Check our pages dedicated to [Scheduling](/workflows/events/schedules)
  and [Triggering flows](/introduction/quickstart/triggering-flows).
</Note>

<CardGroup cols={2}>
  <Card title="States" href="#">
    A state is an object stored as a resource of the resource type `state`
    which is meant to persist across distinct executions of the same script.
  </Card>

  <Card title="Schedules" href="/workflows/events/schedules">
    Scheduling allows you to define schedules for Scripts and Flows,
    automatically running them at set frequencies.
  </Card>

  <Card title="For loops" href="/workflows/events/loops">
    Iterate a series of tasks.
  </Card>
</CardGroup>

<Tip>
  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
  getOperate, **a Trigger Script has the job to keep track of what's processed and
  what's not**.
</Tip>

Flows can be scheduled through the Flow UI using a CRON expression and then
activating the schedule as seen in the image below.

Example of a trigger script watching new Slack posts with a given word in a given channel and
the flow sending each of them by email in a for loop:

> This flow can be found on [getOperate Hub](#).

Examples of trigger scripts include:

* [Trigger every time a new item text on HackerNews match at least one mention](#)
* [Notify of new Github repo stars](#)
* [Check new uploaded files on Google Drive](#)

The following TypeScript code is an example of the first module of a Flow that
checks for new documents in a MongoDB collection on a regular schedule. In this
case we query documents that were created after a specific time, expressed with
a timestamp. The timestamp is stored with the help of getOperate's built-in
[state functions](#) and is
updated in each run.

<Accordion title="Code below:">
  ```ts theme={null}
  import { getState, type Resource, setState } from 'https://deno.land/x/getOperate/mod.ts';
  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 } });
  }
  ```
</Accordion>

<Tip>
  You can find this exact Trigger Script on [getOperate Hub](#), or many more examples [here](#).
</Tip>
