# Create a Genie task

> Gives a Genie a task to run once (run_at) or on a cron schedule (UTC). An editor role is required: a task makes the genie act in the apps it is connected to.

Source: https://docs.stacksync.com/api-reference/genies/tasks/create_genie_task

`POST https://api.stacksync.com/v1/workspaces/{workspace_id}/genies/{genie_id}/tasks`

- Operation: `create_genie_task`
- Resource: Genie tasks
- Access: writes data

## Authentication

- `Authorization: Bearer <key>`: A workspace API key (workspace_…, created by a workspace owner under Workspace settings > Stacksync API) acts with editor rights on that workspace only; a user API key (user_…, created under Profile > Stacksync API) acts as that user in every workspace they belong to. POST, PUT and PATCH requests must send Content-Type: application/json. Every authentication failure answers 403.
- `Content-Type: application/json` on every request with a body.

## Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `workspace_id` | integer | Yes | The workspace. List the ones your key can reach with GET /v1/workspaces. |
| `genie_id` | string (uuid) | Yes | The Genie. |

## Query parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `region` | string (besg \| usnv) | Yes | Region of the genie: besg (Europe) or usnv (United States) |

## Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string \| null | No | Makes the create retry-safe for 24 hours: a retry with the same key answers the resource the first call created, with the Idempotent-Replayed: true header, and creates nothing. 1 to 255 visible ASCII characters. The same key with a different request answers 422; while the first call still runs, 409. |

## Request body

`application/json`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string, 1 to 255 characters | Yes | Unique among the workspace's cron tasks of the region |
| `instruction` | string, at least 1 characters | Yes | What the genie is asked each time the task runs |
| `cron_expression` | string \| null | No | Run on this cron schedule (UTC), e.g. '0 8 * * 1-5'; or run_at |
| `run_at` | string (date-time) \| null | No | Run once at this time (future), ISO 8601; or cron_expression |
| `start_time` | string (date-time) \| null | No | With cron_expression: the schedule starts after this time |
| `is_shared_with_workspace` | boolean, default true | No |  |
| `validate_only` | boolean, default false | No | Run every check of the write and answer 204 without writing: a refused write answers the error the write would |

## Returns

### 201 Created

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `task_id` | string | Yes |  |
| `name` | string \| null | No |  |
| `instruction` | string \| null | No | What the genie is asked each time |
| `cron_expression` | string \| null | No |  |
| `run_at` | string \| null | No | For a one-time task: when it runs, ISO 8601 |
| `start_time` | string \| null | No | For a scheduled task: when the schedule starts |
| `next_run_time` | string \| null | No |  |
| `is_paused` | boolean \| null | No |  |
| `is_shared_with_workspace` | boolean \| null | No |  |
| `created_time` | string \| null | No |  |

### 204 No Content

No body.

## Example request

```bash
curl -X POST "https://api.stacksync.com/v1/workspaces/{workspace_id}/genies/{genie_id}/tasks?region=besg" \
  -H "Authorization: Bearer $STACKSYNC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "cron_expression": "0 8 * * 1-5",
  "instruction": "Summarize the deals that changed stage yesterday",
  "name": "Weekday sales summary"
}'
```

## Example response (201)

```json
{
  "task_id": "<task_id>",
  "name": "<name>",
  "instruction": "<instruction>",
  "cron_expression": "<cron_expression>",
  "run_at": "<run_at>",
  "start_time": "<start_time>",
  "next_run_time": "<next_run_time>",
  "is_paused": false,
  "is_shared_with_workspace": false,
  "created_time": "<created_time>"
}
```

## Errors

| Status | Meaning | Body |
| --- | --- | --- |
| 400 | Bad Request | `{ "error": string, "msg": string, "field": string \| null }` |
| 403 | Forbidden | `{ "error": string, "msg": string, "field": string \| null }` |
| 404 | Not Found | `{ "error": string, "msg": string, "field": string \| null }` |
| 409 | Conflict | `{ "error": string, "msg": string, "field": string \| null }` |
| 422 | Unprocessable Content | `{ "error": string, "msg": string, "field": string \| null }` |

## Related

- [API Reference overview](https://docs.stacksync.com/api-reference): base URLs, regions, pagination, idempotency and errors
- [Authentication](https://docs.stacksync.com/api-reference/authentication): API keys and the 403 answers
- OpenAPI document: https://docs.stacksync.com/openapi.json
- [List Genie tasks](https://docs.stacksync.com/api-reference/genies/tasks/list_genie_tasks)
- [Get a Genie task](https://docs.stacksync.com/api-reference/genies/tasks/get_genie_task)
- [Update a Genie task](https://docs.stacksync.com/api-reference/genies/tasks/update_genie_task)
- [Delete a Genie task](https://docs.stacksync.com/api-reference/genies/tasks/delete_genie_task)