# Write a task

Define a task, a function in your project that does one job and returns a result, then run it with a payload: by hand, on a schedule or from a webhook.

## Before you start

You need a project. See [Create a project](/docs/projects/create-project.md).

## Write a task

**Studio**

1. In your project, open **Building blocks** > **Tasks**.

2. Click **Create task**.

3. Enter `word-count` as the **Name**.

4. Enter `Count the words in a text.` as the **Description**.

5. Replace the **Source code** with:

   ```ts
   export default {
     name: "word-count",
     description: "Count the words in a text.",
     schedulable: true,
     async run({ input }) {
       return { words: String(input.text).trim().split(/\s+/).length };
     },
   };
   ```

6. Click **Create task**.

7. Open the **⋯** menu.

8. Click **Run task**.

9. Paste the **Payload**:

   ```json
   { "text": "Veryfront runs agents, tasks and workflows" }
   ```

10. Click **Create run**. You should now see the run **Completed**, with `{ "words": 6 }` as its **Output**.

### Troubleshooting

**If Studio says "Created, but the source couldn't be saved"**, the task and its source are saved. Carry on with step 7.

**Code**

1. Add the task. Its file name is its ID:

```ts title="tasks/word-count.ts"
export default {
  name: "word-count",
  description: "Count the words in a text.",
  schedulable: true,
  async run({ input }) {
    return { words: String(input.text).trim().split(/\s+/).length };
  },
};
```

2. Run it on your machine. `--config` is the payload:

```bash
npx veryfront task word-count --config '{"text": "Veryfront runs agents, tasks and workflows"}'
```

3. Read the result: it prints `"words": 6`.
4. Start the project with `npm run dev` ([Work locally](/docs/projects/local.md)) and open **Building blocks** > **Tasks** in Studio. You should now see `word-count` listed.

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest word-counter -- --template ai-agent --yes
step: Add the task | cd word-counter && mkdir -p tasks && printf '%s\n' 'export default {' '  name: "word-count",' '  description: "Count the words in a text.",' '  schedulable: true,' '  async run({ input }) {' '    return { words: String(input.text).trim().split(/\s+/).length };' '  },' '};' > tasks/word-count.ts
check: npx veryfront task word-count --config '{"text": "Veryfront runs agents, tasks and workflows"}'
expect: "words": 6
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Save the task's source as `word-count.ts`, the file in the **Code** tab's step 1.
3. Write it to `tasks/word-count.ts` with your [API key](/docs/authentication/api-keys.md). `jq` sends it as `content`; `%2F` is the `/`:

```bash
jq -n --rawfile content word-count.ts '{content: $content}' | \
  curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/tasks%2Fword-count.ts \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @-
```

4. List the tasks. You should now see `word-count`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/tasks \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

5. Get the project. A run takes its `id`, not the slug:

```bash
curl https://api.veryfront.com/projects/$PROJECT \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

6. Set `PROJECT_ID` to the `id` in the response: `export PROJECT_ID=<id>`.
7. Run the task with the payload:

```bash
curl -X POST https://api.veryfront.com/runs \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: word-count-1" \
  -d '{
    "project_id": "'$PROJECT_ID'",
    "target": { "type": "task", "id": "word-count" },
    "input": { "text": "Veryfront runs agents, tasks and workflows" }
  }'
```

8. Set `RUN_ID` to the `run_id` in the response: `export RUN_ID=<run_id>`.
9. Read the run until `status` is `completed`. You should now see `output` as `{"words":6}`:

```bash
curl https://api.veryfront.com/runs/$RUN_ID \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Save the task's source as word-count.ts | printf '%s\n' 'export default {' '  name: "word-count",' '  description: "Count the words in a text.",' '  schedulable: true,' '  async run({ input }) {' '    return { words: String(input.text).trim().split(/\s+/).length };' '  },' '};' > word-count.ts
step: Write it to tasks/word-count.ts | jq -n --rawfile content word-count.ts '{content: $content}' | curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/tasks%2Fword-count.ts -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" --data-binary @-
step: List the tasks | curl https://api.veryfront.com/projects/$PROJECT/tasks -H "Authorization: Bearer $VERYFRONT_API_KEY"
step: Get the project | curl https://api.veryfront.com/projects/$PROJECT -H "Authorization: Bearer $VERYFRONT_API_KEY"
step: Set PROJECT_ID | export PROJECT_ID=$(curl -s https://api.veryfront.com/projects/$PROJECT -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r .id)
step: Run the task | curl -X POST https://api.veryfront.com/runs -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -H "Idempotency-Key: word-count-1" -d '{ "project_id": "'$PROJECT_ID'", "target": { "type": "task", "id": "word-count" }, "input": { "text": "Veryfront runs agents, tasks and workflows" } }' | tee run.json
step: Set RUN_ID | export RUN_ID=$(jq -r .run_id run.json)
step: Wait until it is completed | until [ "$(curl -s https://api.veryfront.com/runs/$RUN_ID -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r .status)" = completed ]; do sleep 3; done
check: curl https://api.veryfront.com/runs/$RUN_ID -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "words":6
```

## With MCP

`my-project` stands for your project's reference: its slug or its ID, both in the answer of `veryfront__create_project` or `veryfront__list_projects`.

1. Call `veryfront__create_file` with `"project_reference": "my-project"`, `"path": "tasks/word-count.ts"` and the task's source from the Code tab as `content`.
2. Call `veryfront__list_tasks` with `"project_reference": "my-project"`. It worked when `word-count` is listed.
3. Call `veryfront__create_run` with the project's `id` (a run takes the ID, not the slug):

```json
{
  "input": {
    "project_id": "<id>",
    "target": { "type": "task", "id": "word-count" },
    "input": { "text": "Veryfront runs agents, tasks and workflows" }
  },
  "idempotency_key": "word-count-1"
}
```

4. Call `veryfront__get_run` with the returned `run_id` until `status` is `completed`. It worked when `output` is `{"words":6}`.

## Next steps

* [Schedule a run](/docs/orchestration/schedules.md)
* [Start a run](/docs/orchestration/start-run.md)
