Skip to Content

Create session

Required accessTasks: triggertrigger:tasksAny key with this access, from API keys
POST
/api/v1/sessions

Create a Session and trigger its first run in one atomic call. A Session is the durable identity for a bi-directional stream of records (the .in and .out channels) that survives across the runs processing it.

Idempotent on externalId within an environment. Calling create again with an externalId that already maps to an open session returns the existing session with isCached: true and 201 becomes 200. Reusing an externalId whose session is already closed or expired returns 409.

Authorize with a secret key, or a public token carrying write:sessions for the session you are creating.

Authorization

headerAuthorizationBearer <token>

Use a named environment API key. It starts with tr_dev_sk_, tr_prod_sk_, tr_stg_sk_, etc.

Create a named API key in the API Keys section of your Trigger.dev project dashboard. Choose the narrowest access preset that supports your integration.

Our TypeScript SDK will default to using the value of the TRIGGER_SECRET_KEY environment variable if it is set. If you are using the SDK in a different environment, you can set the key using the configure function.

import { configure } from "@trigger.dev/sdk";configure({ accessToken: "tr_dev_sk_1234" });

Request Body

application/json
  1. body

Body for POST /api/v1/sessions. The whole body must be 32KB or smaller.

type*string

Free-form discriminator for the session, e.g. chat.agent. Not validated against an enum.

Length1 <= length <= 64
taskIdentifier*string

The task this session triggers runs against.

Length1 <= length <= 128
triggerConfig*

Trigger options applied to every run a session schedules. basePayload is the wire payload merged into each run; the remaining fields map onto the standard trigger options.

externalId?string

Your stable identity for the session, unique per environment. Cannot start with the reserved session_ prefix. Reusing an externalId makes create idempotent; reusing one whose session is closed or expired returns 409.

Length1 <= length <= 256
tags?array<>

Up to 10 tags on the session row, for dashboard filtering.

Itemsitems <= 10
metadata?

Arbitrary JSON metadata.

expiresAt?string

Absolute expiry timestamp for retention.

Formatdate-time

Response Body

An open session already existed for this externalId. The existing session is returned with isCached: true.

application/json
  1. response

A session row.

id*string

The session's friendly ID, prefixed with session_.

externalId?string

Your stable identity for the session, if one was set.

type*string

The session type discriminator.

taskIdentifier*string

The task this session triggers runs against.

triggerConfig?

Trigger options applied to every run a session schedules. basePayload is the wire payload merged into each run; the remaining fields map onto the standard trigger options.

currentRunId?string

Friendly ID of the live run for this session, if any. Prefixed with run_. Omitted on list rows.

tags*array<string>

Tags on the session row.

metadata?

Arbitrary JSON metadata, or null if unset.

closedAt?string

When the session was closed, or null if open.

Formatdate-time
closedReason?string

The optional reason recorded when the session was closed.

expiresAt?string

The session's retention deadline, or null if none.

Formatdate-time
createdAt*string
Formatdate-time
updatedAt*string
Formatdate-time
runId*string

Friendly ID of the first run triggered alongside the session.

publicAccessToken*string

Session-scoped public access token carrying read:sessions:{key} and write:sessions:{key}. Default TTL is 1 hour. Safe to pass to frontend clients.

isCached*boolean

true if an open session already existed for this externalId (idempotent upsert), false if newly created.

Example Request
import { sessions } from "@trigger.dev/sdk";const { id, runId, publicAccessToken, isCached } = await sessions.start({  type: "chat.agent",  externalId: chatId,  taskIdentifier: "my-chat",  triggerConfig: {    basePayload: { chatId },    tags: [`chat:${chatId}`],  },});console.log(id);       // e.g. "session_abc123"console.log(runId);    // the first run, e.g. "run_def456"console.log(isCached); // false on a brand-new session
Example Response
{  "id": "session_abc123",  "externalId": "chat_1234",  "type": "chat.agent",  "taskIdentifier": "my-chat",  "triggerConfig": {    "basePayload": {},    "machine": "small-1x",    "queue": "string",    "tags": [      "string"    ],    "maxAttempts": 1,    "maxDuration": 1,    "lockToVersion": "20240523.1",    "region": "string",    "idleTimeoutInSeconds": 1  },  "currentRunId": "run_def456",  "tags": [    "chat:1234"  ],  "metadata": {},  "closedAt": "2019-08-24T14:15:22Z",  "closedReason": "string",  "expiresAt": "2019-08-24T14:15:22Z",  "createdAt": "2019-08-24T14:15:22Z",  "updatedAt": "2019-08-24T14:15:22Z",  "runId": "run_def456",  "publicAccessToken": "string",  "isCached": true}

Was this page helpful?

Last updated

Edit on GitHub