Docs

Exchange a handoff token for a session

POST
/sessions/exchange

Exchanges a handoff token for a session and a session_token.

A handoff token is the short-lived, single-use credential that createAuthAttemptHandoff (POST /auth_attempts/{attempt_id}/handoff) returns as handoff_token once an authentication attempt is complete. This call consumes it. It is the last step of the sign-in sequence: create the attempt, issue a challenge, verify the proof, create the handoff, exchange it.

The server resolves the originating authentication attempt from the token and then:

Originating authentication attemptOutcome
No session_idA new authenticated session is created.
session_id points to an anonymous sessionExisting session is upgraded: user and factors written in, lifetime reset to the full session lifetime.
session_id points to an active session (step-up)Existing session is upgraded: new factors merged, assurance_levels[] expanded.

The response shape is identical in all three cases.

The session_token supersedes any previously issued session_token for the same session. Clients must replace their stored token at this point.

Requires a project service key (OAuth2 client credentials). The project comes from the project secret. A query project_id that disagrees answers 403 with proj.mismatch.

The exchange resolves a deployment the same way as other runtime operations. A deployment makes a release live, and a release is the configuration that zitadel deploy publishes. With no browser Origin and nothing deployed to the resolved target, the response is 409 with rel.not_deployed: run zitadel deploy, then retry. A revoked release answers 409 with rel.revoked.

Authorization

oauth2 session.write
headerAuthorizationBearer <token>

Scope: session.write

Query Parameters

project_id*string

The project ID: the id that createProject (POST /projects) returns. zitadel setup prints it as "Project id" in its summary and as project_id with --json, and writes it to .env.local as ZITADEL_PROJECT_ID.

Match^[a-zA-Z0-9_-]+$

Header Parameters

Idempotency-Key?string

Optional retry key. The server accepts it but does not use it: a handoff token exchanges once, and a retry with the same key answers 400 sess.invalid_handoff_token.

Request Body

application/json
  1. body

Request to exchange a handoff token for a session and session_token.

handoff_token*string

The one-time handoff token that createAuthAttemptHandoff (POST /auth_attempts/{attempt_id}/handoff) returns as handoff_token. It is single-use. Replaying it after a successful exchange returns 400 with sess.invalid_handoff_token, the same answer as for an unknown or expired token.

ttl?string

Optional session lifetime after exchange, as an ISO-8601 duration. When omitted, the server uses the configured default. It must not exceed the configured maximum.

Formatduration

Response Body

The session and its new session_token.

application/json
  1. response

The response of session creation and handoff exchange. It holds the session state and the session_token required to manage it.

session*

The current state of a session.

A session is the durable record of a user who has signed in. It accumulates verified authentication factors and exposes assurance_levels[], the set of assurance profiles its current factors satisfy. Sessions are read-only for the client: factors flow in through authentication attempts.

session_token*string

Bearer token authorising management of this session (GET and DELETE). For anonymous sessions this token is superseded when a handoff exchange completes. Clients must replace their stored token at that point.

Example Request
import { createZitadelApi, ZitadelClient } from "@zitadel/api/client";const api = createZitadelApi(  new ZitadelClient({    baseUrl: process.env.ZITADEL_URL ?? "http://localhost:8080",    token: process.env.ZITADEL_PROJECT_SECRET!, // server-side only  }),);const handoff = await api.exchangeHandoff({ handoff_token: "string" }, { project_id: "proj_01KWH1P4MYS7F0N9WD3P2E4YM5" });
Example Response
{  "session": {    "session_id": "string",    "project_id": "string",    "state": "building",    "user_id": "string",    "user": {      "user_id": "string",      "identifier": "string",      "identifier_property": "string",      "display": "string"    },    "factors": [      {        "method": "identifier",        "verified_at": "2019-08-24T14:15:22Z",        "payload": {          "method": "identifier",          "user_id": "string"        }      }    ],    "assurance_levels": [      "string"    ],    "metadata": {},    "user_agent": {      "fingerprint": "string",      "ip": "string"    },    "created_at": "2019-08-24T14:15:22Z",    "expires_at": "2019-08-24T14:15:22Z"  },  "session_token": "string"}

Was this helpful?