Skip to content
SecondStack SecondStack Docs

Call SecondGate from your code

Updated · Checked against SecondStack v3.7.2This page as Markdown
On this page
Labels on this page
virtual key
API Keys
the OpenAI-compatible base URL
OpenAI-compatible base URL (SDKs and CLIs)
the Anthropic base URL
Anthropic base URL (Claude Code)
the record (keys)
Usage Analytics & Logs
the record (agents)
Agent Activity
model access
Access via direct API

Call SecondGate from your code with a virtual key: the OpenAI-compatible and Anthropic endpoints under /gateway/api, the subscription route and refusals. Your code calls SecondGate the way it would call a provider. SecondGuard and SecondGate apply your rules, model access and budget before the request leaves.

Where the gateway is

The gateway answers at /gateway/api on your platform’s host, over HTTPS, and the examples below use https://ai.example.com in its place.

Two families of endpoint sit under it at v3.7.2:

  • OpenAI-compatible, under /gateway/api/v1/: chat/completions, responses, responses/compact, embeddings, audio/transcriptions, images/generations, images/edits, models and model/info.
  • Anthropic Messages, under the same prefix: messages and messages/count_tokens. The API Keys page shows https://ai.example.com/gateway/api as the Anthropic base URL (Claude Code) and https://ai.example.com/gateway/api/v1 as the OpenAI-compatible base URL (SDKs and CLIs).

Gemini’s own generateContent, streamGenerateContent, countTokens and embedContent answer under /gateway/api/v1beta/models/<model>:. A search endpoint serves the gateway’s search model. Only these inference endpoints are reachable from outside. The control and metrics routes stay inside the deployment.

Get a virtual key

A virtual key starts with sg_, belongs to a person, a team, an application or an agent, and carries its own budget and its own model access.

  1. Open ControlTower. From the Chat App, Settings → Usage and API Access takes you there.
  2. Choose API Keys, then Create New API Key and Create Key. The page says API Key Created Successfully and asks you to copy the key now.
  3. Put the key in your environment. GET /gateway/api/v1/models with the key lists the models it may use. If the list is empty, ask your administrator for a grant under Access via direct API.

A team lead creates keys for applications and background jobs under Apps & Agents Keys, each with its own budget. A key stops working when you delete it, and Delete API key? asks first.

Send a request

Send the request as you would to the provider, with the virtual key as the bearer token and the gateway as the base URL.

bash
curl https://ai.example.com/gateway/api/v1/chat/completions \
  -H "Authorization: Bearer $SECONDGATE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6.1-terra",
    "messages": [
      { "role": "user", "content": "Summarize this vendor review in five lines." }
    ]
  }'

Any OpenAI SDK works the same way: set the base URL to https://ai.example.com/gateway/api/v1 and the API key to the virtual key.

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.example.com/gateway/api/v1",
    api_key=os.environ["SECONDGATE_KEY"],
)
reply = client.chat.completions.create(
    model="gpt-6.1-terra",
    messages=[{"role": "user", "content": "Summarize this vendor review in five lines."}],
)

The model name is the id your site defines, as the models endpoint lists it. The default site ships gpt-6.1-terra. SecondGate fails over across a model’s provider bindings by priority, up to the attempts your site allows.

The subscription route

Models your organization reaches through a ChatGPT or Claude subscription pool answer under /gateway/api/subscription/v1/, with the same request shape.

A request on the subscription route debits no budget, and the record still shows the usage. SecondGate spreads the pool’s requests in round robin. The two routes never fall back to each other, so a subscription model asked for on /v1/ returns an error. The Gemini endpoints have no subscription route. Model providers explains how an administrator sets up a pool.

What the record keeps

Every provider attempt writes one row to the ledger, and ControlTower shows yours under Usage Analytics & Logs and an agent’s under Agent Activity.

Request record· ControlTower · Usage Analytics & Logs
sg_…4f2a 6 Nov 2026 · 14:02:11 · 1.8 s
Keysg_…4f2a · Finance team
Modelgpt-6.1-terra · API route
Tokens1,284 in · 640 cache read · 212 out
Cost$0.0146 · debited to Finance team
Budget$112.40 of $500 this month
Tagsapp=vendor-review
Read the full record under Usage Analytics & Logs
One request, one record: which key called, which model answered, what it cost and which budget paid.

A row holds the key and its billing scope, the model and the provider model, the tokens, the priced value, the budget debit and the request tags your client sent. A failed attempt stays visible beside the one that succeeded. SecondGate keeps neither the prompt nor the response body.

When a request is refused

SecondGate fails closed and returns an error when a key has no grant on the model, when a budget is spent or when the model does not answer on the route you called.

SecondGate passes provider errors back unchanged. The platform rewords only errors about your organization’s provider account, such as a reached spend cap, and keeps the provider’s message after its own text. SecondGate does not retry a refused request on another model.

What the gateway API does not do

  • The gateway does not fall back between /v1/ and /subscription/v1/. Your code picks the route the model lives on.
  • The gateway does not pick a default model for you. A request never reaches a model the key has no grant for.
  • SecondGate stores no prompts, responses or media. A client that needs its history keeps it itself.
  • Request tags attribute traffic in the record and grant nothing.
  • This page does not cover the Agents API. That is a separate surface under /agents/api/v1, and it is off by default.