Skip to main content

How it works

ServerlessAI is an API Gateway that authenticates users, detects abuse, enforces quotas, and routes requests to specified providers based on your rules.

Making API Calls

ServerlessAI provides OpenAI-compatible endpoints at https://openai.api.serverlessai.dev. To authenticate, use a bearer token in the format: <Your Project ID>:<User's JWT token>.

JavaScript / TypeScript Example

First, install the OpenAI package using your preferred package manager:

npm install openai

Next, construct the OpenAI client:

const client = new OpenAI({
baseURL: ENDPOINT,
apiKey: `${PROJECT_ID}:${getUserJWT()}`,
dangerouslyAllowBrowser: true,
}),

Swift Example

For Swift, you can use a community-maintained OpenAI client, such as MacPaw's OpenAI client::

import OpenAI

let client = OpenAI(
configuration: OpenAI.Configuration(
token: "\(PROJECT_ID):\(getUserJWT())",
host: "openai.api.serverlessai.dev",
)
)