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

# Working with routines

> Combine one trigger with ordered steps, then inspect runs, outputs, and delivery.

A routine combines **one trigger (When)** with **ordered Steps** and their **results: Runs, Output, and Delivery**. A [Serverless Function](https://docs.mobilerun.ai/routines/functions.md) is one kind of step and can also run on its own. A [task](https://docs.mobilerun.ai/tasks.md) asks an AI agent to carry out an instruction and can be a routine step too.

```mermaid theme={null}
flowchart LR
  A[When: one trigger] --> B[Steps: ordered actions]
  B --> C[Runs and Output]
  C --> D[Delivery]
```

Use a manual or scheduled trigger today; app, device, and platform event triggers are **Soon**. Steps can target devices when needed. The API calls routines **flows** (`/v1/flows`) and their runs **executions** (`/v1/executions`).

## Step kinds

| Step | What it does |
| - | - |
| [Serverless Function](https://docs.mobilerun.ai/routines/functions.md) | Runs uploaded, versioned Python code with typed inputs and returns. |
| Agent task | Asks an agent to carry out an instruction (`tasks.run` or `agent.run`). |
| Files and datasets | Works with files or compares datasets. |
| Drive upload | Uploads files to Google Drive. |
| Webhook | Sends a request to an external endpoint. |
| Loop | Repeats steps with `workflow.loop`. |

A device script (`scripts.run`) and an uploaded Serverless Function (`functions.run`) are different step kinds. Available steps come from the action catalog, not every connected app; see [Integrations and delivery](https://docs.mobilerun.ai/routines/integrations.md).

## Create a routine

**1. Start in Cloud or ask your agent**

Open **Routines**, choose **New routine**, enter a **Name**, and choose **Create routine**. Add or edit steps on **Steps**; changes autosave. Or [paste a document](https://docs.mobilerun.ai/routines/documents.md#quick-start) to configure the whole routine. You can also [ask your agent to create a routine](https://docs.mobilerun.ai/agents/workflows.md).
Open Routines

**2. Choose When and Steps**

Open **When** and choose **Manually** or **On a schedule**. Add a ready Serverless Function or an action from the catalog, configure inputs, and choose a device if needed. **On an event** is **Soon**.

**3. Check, Test run, then enable**

**Check** inspects configuration without executing work. **Test run** performs real work: inspect its result before enabling recurring runs. A routine without steps stays disabled. See [Runs and troubleshooting](https://docs.mobilerun.ai/routines/runs.md).

### Through the API or SDK

Use your [API key](https://docs.mobilerun.ai/api-keys.md). The SDK examples use `@mobilerun/sdk` or `mobilerun-sdk` **6.2.1**; `mobilerun.api` accesses their typed HTTP methods and response envelopes. The example assumes you already have a trigger, an action, and a connected device: replace their UUIDs below. Create a trigger through `POST /v1/triggers` and an action through `POST /v1/actions`; the [function walkthrough](https://docs.mobilerun.ai/agents/functions.md#run-as-a-routine-step-through-the-api) shows how to connect a Serverless Function.

**1. Create a disabled routine**

Bind the trigger to ordered actions. Keep `enabled: false` while you check the configuration. An idempotency key lets you safely retry the same create request.

**2. Check and Test run**

[Validate and dry-run](https://docs.mobilerun.ai/routines/runs.md#check-vs-test-run), then verify with a real run. Inspect the execution before activating the unchanged routine.

**3. Enable the routine**

Use the [verified activation path](https://docs.mobilerun.ai/routines/runs.md#enable) to enable recurring work after successful verification.

```typescript TypeScript theme={null}
import { MobilerunSDK } from '@mobilerun/sdk';

const mobilerun = new MobilerunSDK();
const routine = await mobilerun.api.workflows.flows.create({
  name: 'Daily app check',
  triggerId: '11111111-1111-4111-8111-111111111111',
  deviceIds: ['22222222-2222-4222-8222-222222222222'],
  actions: [{ actionId: '33333333-3333-4333-8333-333333333333', position: 0 }],
  enabled: false,
}, { headers: { 'Idempotency-Key': crypto.randomUUID() } });
console.log(routine.data.id);
```

```python Python theme={null}
from uuid import uuid4
from mobilerun_sdk import MobilerunSDK

mobilerun = MobilerunSDK()
routine = mobilerun.api.workflows.flows.create(
    name="Daily app check",
    trigger_id="11111111-1111-4111-8111-111111111111",
    device_ids=["22222222-2222-4222-8222-222222222222"],
    actions=[{"action_id": "33333333-3333-4333-8333-333333333333", "position": 0}],
    enabled=False,
    idempotency_key=str(uuid4()),
)
print(routine.data.id)
```

```bash cURL theme={null}
curl --fail-with-body https://api.mobilerun.ai/v1/flows \
  -H "Authorization: Bearer $MOBILERUN_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"name":"Daily app check","enabled":false,
    "triggerId":"11111111-1111-4111-8111-111111111111",
    "deviceIds":["22222222-2222-4222-8222-222222222222"],
    "actions":[{"actionId":"33333333-3333-4333-8333-333333333333","position":0}]}'
```

Describe the whole routine as one [YAML or JSON document](https://docs.mobilerun.ai/routines/documents.md).

## Explore routines

> [card] [Triggers](https://docs.mobilerun.ai/routines/triggers.md)
>
> Choose When: manual, schedule, or a custom API call. Event triggers are Soon.

> [card] [Steps and Functions](https://docs.mobilerun.ai/routines/functions.md)
>
> Build steps and upload, version, or run Serverless Functions.

> [card] [Documents (YAML/JSON)](https://docs.mobilerun.ai/routines/documents.md)
>
> Copy a template, edit the whole routine, and check it before saving.

> [card] [Integrations and delivery](https://docs.mobilerun.ai/routines/integrations.md)
>
> Connect apps, deliver files, and notify on outcomes.

> [card] [Runs and troubleshooting](https://docs.mobilerun.ai/routines/runs.md)
>
> Check, test, monitor results, and fix failing routines.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.