Create an anonymous session
Creates an anonymous session with no user and no factors (state: building).
A session is the durable record of a signed-in user. This call is optional: an authentication attempt creates a session implicitly if none is provided. Create one explicitly when you want to:
- Pre-allocate a
session_idbefore the user is known, so device and telemetry signals can be correlated with the eventual authenticated session from the start. - Track anonymous state (bot detection, device fingerprint) that survives until authentication.
The session belongs to the project of the project secret. A project_id that
names another project answers 403 with proj.mismatch and creates nothing:
send the credential's own project.
Creating a session runs on the project credential (session.write). The returned
session_token is a session credential for the end-user client, not a
management scope. It is delivered as the __Host-zitadel_session cookie
(zitadel_session over plain http to a loopback host) and authorises the
self-service operations GET /sessions/me and DELETE /sessions/me
(sessionCookie scheme). The by-id operations GET /sessions/{session_id}
and DELETE /sessions/{session_id} are operator endpoints and require
session.read / session.delete instead.
The session_token is superseded when a handoff exchange completes. Clients
must replace it at that point.
Anonymous sessions expire after 10 minutes. The lifetime resets to the configured full session lifetime when a completing authentication attempt writes the first authentication factor.
oauth2 session.writeAuthorizationBearer <token>Scope: session.write
application/json- body
Request to create an anonymous session.
project_id*project-id^[a-zA-Z0-9_-]+$user_agent?Optional user agent / device context to attach to the session. Useful for correlating telemetry and device signals with the eventual authenticated session from the start.
The anonymous session and its session_token.
application/json- 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*stringBearer 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.
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 result = await api.createSession({ project_id: "string" });{ "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?