Skip to content
Call from a browser or app

Call from a browser or app

Issue a short-lived credential so a browser or mobile app can use one Rebyte Agent Session directly.

On this pageCreate the Session on your serverIssue a client secretStream a reply from the clientLimits, billing, and revocation

Use a Session client secret when your browser or mobile app needs to send messages and stream output directly from Rebyte. Your backend or server-side loader authenticates the user and issues the secret with its organization API key. Keep the organization key on the server.

Create the Session on your server

Create or look up a Session that belongs to the signed-in user. To create one before the first message, send POST /v1/agents/sessions to https://api.rebyte.ai with Authorization: Bearer <organization_api_key>, OpenAI-Beta: agents=v1, and Content-Type: application/json. Give the Session a credit budget and set defer_input to true.

json
{
  "agent": {
    "model": "grok-4.7",
    "instructions": "Answer concisely.",
    "budget": { "max_credits": 20 }
  },
  "environment": { "type": "none" },
  "defer_input": true
}

Save the returned Session ID with the user in your app. On retries, reuse the same Idempotency-Key and body. Client access requires a Session without a Sandbox, Vault bindings, MCP, or Workflow tools. It supports function tools, web search, tool search, Decision, and hosted media tools. The same client secret can download media generated in that Session.

Issue a client secret

Send POST /v1/agents/sessions/{session_id}/client_secrets with the same headers. The organization key needs tasks:read and tasks:write.

json
{
  "user": "your-stable-app-user-id",
  "expires_after": { "anchor": "created_at", "seconds": 1800 }
}

Set user on the server from the authenticated identity. The first issuance binds the Session to that user. Every later issuance for the Session must use the same ID.

The response contains id, session_id, client_secret, and expires_at in Unix seconds. The default lifetime is 30 minutes. The maximum is one hour, and the secret expires no later than the issuing key. Return the Session ID, secret, and expiry over HTTPS with Cache-Control: no-store. Keep the secret in memory on the client. Request a fresh secret from your authenticated server before expiry or after a 401 caused by an expired secret.

Stream a reply from the client

This example uses the official openai JavaScript SDK (7.64.0). Pass the short-lived client_secret as apiKey and subscribe before sending input.

javascript
import OpenAI from 'openai';

async function sendMessage({ session_id, client_secret }, text, onDelta) {
  const client = new OpenAI({
    apiKey: client_secret,
    baseURL: 'https://api.rebyte.ai/v1',
    dangerouslyAllowBrowser: true,
    maxRetries: 0,
    fetchOptions: { credentials: 'omit' },
  });
  const sessions = client.beta.agents.sessions;
  const stream = await sessions.events.stream(session_id);
  try {
    const { turn_id } = await sessions.events.create(session_id, {
      'Idempotency-Key': crypto.randomUUID(),
      events: [{
        type: 'agent.session.input.message',
        input: [{ role: 'user', content: [{ type: 'input_text', text }] }],
      }],
    });
    for await (const event of stream) {
      if (event.type === 'agent.session.turn.output_text.delta' && event.turn_id === turn_id) {
        onDelta(event.delta);
      }
      if (event.type === 'agent.session.turn.completed' && event.turn.id === turn_id) break;
      if ((event.type === 'agent.session.turn.failed' ||
           event.type === 'agent.session.turn.cancelled') && event.turn.id === turn_id) {
        throw new Error(`Turn ${event.type.split('.').at(-1)}`);
      }
    }
  } finally {
    stream.controller.abort();
  }
}

For direct fetch calls, send Authorization: Bearer <client_secret> and OpenAI-Beta: agents=v1, and set credentials: 'omit'. Use GET to read the Session, Items, history, Turns, or event stream. Use POST .../events to send messages or cancel a Turn. To restore a Session after disconnecting, retrieve stored Turns and Items, then subscribe again. Set allow_tool_results: true at issuance only when your app should let the client supply function results.

Limits, billing, and revocation

  • Client requests share a limit of 10 per minute per Session. Honor the Retry-After header on 429 responses.
  • Model calls use the organization's credit balance. Charges are attributed to the organization API key that issued the client secret.
  • To revoke one secret, send POST /v1/agents/sessions/{session_id}/client_secrets/{secret_id}/cancel from your server with the issuing key. Revoking the organization key invalidates every secret it issued.
  • Event streams close at expiry or on the next authorization check after revocation. Already accepted Turns can finish.