Skip to main content
POST
/
v1
/
sessions
Sessions API
curl --request POST \
  --url https://api.statebase.org/v1/sessions

Sessions API

Sessions are the top-level containers for all agent interactions. They persist state, memory, and conversation history.

Authentication

All API requests require authentication via API key:
curl https://api.statebase.org/v1/sessions \
  -H "X-API-Key: sb_your_api_key_here"
Header: X-API-Key: sb_<your_key>
Get your API key: Dashboard → API Keys

Create Session

POST /v1/sessions Creates a new session for an agent. Sessions are isolated containers that store state, memory, and conversation history.

Request Body

FieldTypeRequiredDescription
agent_idstring✅ YesUnique identifier for your agent (e.g., customer-support-v1)
user_idstringNoUser identifier for multi-tenant isolation
initial_stateobjectNoStarting state (default: {})
metadataobjectNoCustom metadata (e.g., {"env": "prod", "version": "1.2"})
ttl_secondsintegerNoSession lifetime in seconds (default: 86400 = 24h, max: 2592000 = 30 days)

Response

FieldTypeDescription
idstringUnique session ID (format: sess_<12_chars>)
agent_idstringAgent identifier
user_idstringUser identifier (if provided)
created_atstringISO 8601 timestamp
updated_atstringISO 8601 timestamp
stateobjectCurrent session state
metadataobjectCustom metadata
memory_countintegerNumber of memories attached to this session
turn_countintegerNumber of conversation turns
ttl_expires_atstringISO 8601 timestamp when session expires

Example

curl -X POST https://api.statebase.org/v1/sessions \
  -H "X-API-Key: sb_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "customer-support",
    "user_id": "user_123",
    "initial_state": {
      "status": "new",
      "priority": "normal"
    },
    "metadata": {
      "channel": "web",
      "region": "us-west"
    },
    "ttl_seconds": 3600
  }'

Error Responses

Status CodeError CodeDescription
400invalid_requestMissing required fields or invalid data types
401authentication_failedInvalid or missing API key
429rate_limit_exceededToo many requests (default: 100/min)
500internal_errorServer error (contact support if persists)

Get Session

GET /v1/sessions/{session_id} Retrieves a session by ID, including current state and metadata.

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSession ID (e.g., sess_a1b2c3d4e5f6)

Response

Same structure as Create Session response.

Example

curl https://api.statebase.org/v1/sessions/sess_a1b2c3d4e5f6 \
  -H "X-API-Key: sb_your_key"

Error Responses

Status CodeError CodeDescription
404session_not_foundSession doesn’t exist or has expired
401authentication_failedInvalid API key

Get Session Context

POST /v1/sessions/{session_id}/context The most important endpoint for building agents. Returns everything your agent needs in one call:
  • Current state
  • Relevant memories (via semantic search)
  • Recent conversation turns

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSession ID

Request Body

FieldTypeRequiredDescription
querystringNoSearch query for semantic memory retrieval (usually the user’s current message)
memory_limitintegerNoMax memories to return (default: 5, max: 20)
turn_limitintegerNoMax recent turns to return (default: 10, max: 50)

Response

FieldTypeDescription
stateobjectCurrent session state
memoriesarrayRelevant memories (sorted by similarity score)
recent_turnsarrayRecent conversation turns (newest first)

Example

curl -X POST https://api.statebase.org/v1/sessions/sess_abc123/context \
  -H "X-API-Key: sb_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "What did I tell you about my preferences?",
    "memory_limit": 5,
    "turn_limit": 10
  }'

Update Session State

POST /v1/sessions/{session_id}/state Updates the session’s state. Creates a new state version for rollback capability.

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSession ID

Request Body

FieldTypeRequiredDescription
stateobject✅ YesNew state (replaces current state entirely)
reasoningstringNoWhy this update was made (for debugging)

Response

Returns updated session object.

Example

curl -X POST https://api.statebase.org/v1/sessions/sess_abc123/state \
  -H "X-API-Key: sb_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "state": {
      "status": "in_progress",
      "current_step": "gathering_requirements"
    },
    "reasoning": "User provided initial requirements"
  }'

Rollback Session

POST /v1/sessions/{session_id}/rollback Reverts session state to a previous version. Creates a new version with the restored state.

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSession ID

Request Body

FieldTypeRequiredDescription
versioninteger✅ YesState version to roll back to (0 = initial state)

Example

curl -X POST https://api.statebase.org/v1/sessions/sess_abc123/rollback \
  -H "X-API-Key: sb_your_key" \
  -H "Content-Type: application/json" \
  -d '{"version": 3}'

Fork Session

POST /v1/sessions/{session_id}/fork Creates a new session starting from a specific version of an existing session. Useful for debugging and “what-if” scenarios.

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSource session ID

Request Body

FieldTypeRequiredDescription
versionintegerNoState version to fork from (default: latest)

Response

Returns new session object with metadata indicating it was forked.

Example

curl -X POST https://api.statebase.org/v1/sessions/sess_abc123/fork \
  -H "X-API-Key: sb_your_key" \
  -H "Content-Type: application/json" \
  -d '{"version": 5}'

List Sessions

GET /v1/sessions Lists all sessions for your account, with pagination.

Query Parameters

ParameterTypeRequiredDescription
limitintegerNoMax sessions to return (default: 20, max: 100)
starting_afterstringNoSession ID to start after (for pagination)
agent_idstringNoFilter by agent ID
user_idstringNoFilter by user ID

Example

curl "https://api.statebase.org/v1/sessions?limit=10&agent_id=customer-support" \
  -H "X-API-Key: sb_your_key"

Delete Session

DELETE /v1/sessions/{session_id} Permanently deletes a session and all associated data (state, turns, memories). Warning: This action is irreversible.

Path Parameters

ParameterTypeRequiredDescription
session_idstring✅ YesSession ID to delete

Example

curl -X DELETE https://api.statebase.org/v1/sessions/sess_abc123 \
  -H "X-API-Key: sb_your_key"

Rate Limits

TierRequests/MinuteBurst
Free100200
Pro1,0002,000
EnterpriseCustomCustom
Rate limit headers (included in all responses):
  • X-RateLimit-Limit: Max requests per minute
  • X-RateLimit-Remaining: Remaining requests
  • X-RateLimit-Reset: Unix timestamp when limit resets

Webhooks

Subscribe to session events:
EventDescription
session.createdNew session created
session.updatedSession state or metadata changed
session.deletedSession deleted
session.expiredSession TTL expired
Configure webhooks in Dashboard → Webhooks.

Best Practices

✅ Do This

  • Use get_context() for every turn (it’s optimized for agent prompts)
  • Include reasoning in state updates (helps with debugging)
  • Set appropriate TTLs (don’t keep sessions forever)
  • Use user_id for multi-tenant isolation

❌ Avoid This

  • Don’t poll for updates (use webhooks instead)
  • Don’t store sensitive data in state (use encrypted metadata)
  • Don’t create a new session per message (reuse sessions for conversations)

Next Steps


Need help? Join our Discord or email support@statebase.org