Serverless Quick Start

Prepare an account, API key and Model ID, then send your first serverless inference request.

About serverless inference

Serverless inference lets you send requests on demand without provisioning capacity yourself. It can suit prototypes, variable traffic, model evaluations and low-frequency tasks. Usage is billed according to the applicable model price and meters. For sustained workloads, consider reserved throughput after confirming actual service availability.

Step 1: Register

Create an individual or company account to access the console.

Register ↗

Step 2: Create an API key

Create and name a key in the console. Use separate keys for projects or environments. Keep the key in an environment variable or a secret manager; do not hard-code or commit it.

Manage API keys ↗

Replace YOUR_API_BASE_URL with your assigned gateway base address; the examples already include /v1, so do not add it twice.

Store your key in an environment variable
# Linux / macOS
export REQUEST_ID="your-request-id"
export TOKENGRID_API_KEY="sk-your-api-key"
export TOKENGRID_BASE_URL="YOUR_API_BASE_URL/v1"

# Windows (PowerShell)
$env:REQUEST_ID="your-request-id"
$env:TOKENGRID_API_KEY="sk-your-api-key"
$env:TOKENGRID_BASE_URL="YOUR_API_BASE_URL/v1"

Step 3: Choose a model

Review available models, capabilities, context lengths and prices in the model catalog. Use the exact Model ID in the request's model field, not the display name.

Browse models ↗

Step 4: Send a request

Use your assigned gateway URL and API key. The following examples use the OpenAI Chat Completions format; supported capabilities depend on the model and enabled service. Select Python, JavaScript or cURL:

Set REQUEST_ID to a unique identifier for each request to help trace it.

Replace YOUR_API_BASE_URL with your assigned gateway base address; the examples already include /v1, so do not add it twice. Setting TOKENGRID_BASE_URL does not change examples with hard-coded addresses; replace the placeholder in those examples separately.

Python · OpenAI SDK
# pip install openai
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TOKENGRID_API_KEY"],
    base_url="YOUR_API_BASE_URL/v1"
)

response = client.chat.completions.create(
    model="YOUR_MODEL_ID",
    messages=[
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user",   "content": "Hello, please introduce yourself briefly."}
    ],
    temperature=0.7,
    max_tokens=512
)

print(response.choices[0].message.content)
print(f"Input tokens:{response.usage.prompt_tokens}")
print(f"Output tokens:{response.usage.completion_tokens}")

Step 5: Read the response

Example response
{
  "id": "chatcmpl-xxxxxxxx",
  "object": "chat.completion",
  "model": "YOUR_MODEL_ID",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! I am an AI assistant on TokenGrid……"
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 42,
    "completion_tokens": 128,
    "total_tokens": 170
  }
}
  • choices[0].message.content contains generated text.
  • finish_reason may indicate a normal stop, a length limit, or a tool call.
  • usage reports token usage when provided by the interface.

Step 6: Review usage

Review available token usage in the console. Data availability and update timing depend on the service.

View usage ↗ · Read pricing →

Status Code Summary

Status CodeMeaningTypical Scenarios
200SuccessThe request succeeded; if the asynchronous task itself fails, this may also be expressed as status: "failed" in the response.
400Client request errorInvalid parameters, missing required fields, failure to parse the request body, or the task does not exist.
401Authentication failedToken is missing, invalid, or disabled; API Key does not exist, is disabled, expired, or unavailable.
402Payment or quota unavailableOverdue payment, insufficient balance, or plan or free quota exhausted.
403Insufficient permissionsUser is banned, lacks access to a group or model, or does not pass IP restrictions.
404Resource not foundModel does not exist, has been deleted, or is not visible to the caller.
405Request method not supportedWrong endpoint called, for example, sending GET when POST should be used.
408Upstream timed out waiting for the requestRequest transmission is too slow, the connection is interrupted, or the complete request is not received in time.
409Current operation conflicts with resource stateA resource with the same name is created again, or the task state does not allow the current operation.
413Request body too largeThe request body exceeds the size limit.
415Content type not supportedIncorrect Content-Type, or unsupported file or media format.
422Content cannot be processedInvalid field types, message structure, or business parameter semantics.
429Request rate limitedModel request rate limiting, global API rate limiting, or upstream load saturation.
500Internal server errorRequest conversion, upstream call, response parsing, or serialization failed.
501Not implementedThe interface or conversion has not yet been implemented.
502Gateway errorUpstream dependency failure, invalid response format, or failure to fetch the upstream URL in the video content proxy.
503Service unavailableNo available channel, overloaded system resources, insufficient capacity, or service not ready.
504Upstream timeoutThe channel response time exceeded the limit.

Next steps