Skip to content

OpenAI SDK compatibility ​

LAYSO AI provides OpenAI-compatible endpoints. An existing OpenAI SDK project usually only needs a new Base URL and API key, plus the exact model ID shown on the LAYSO AI pricing page.

One key for every platform model

One LAYSO AI API key can access every model available on the platform. Billing follows the selected model's current price. Every successful request has an itemized charge, while failed tasks are not charged.

Before you begin ​

  1. Create and copy a key on the API Keys page.
  2. Choose a model on the Models and pricing page, then copy the exact model ID displayed there.
  3. Confirm that your server can reach https://api.layso.ai.
SettingLAYSO AI value
Base URLhttps://api.layso.ai/v1
API key environment variableLAYSO_API_KEY
Model<MODEL_ID> from the Models and pricing page

Do not place a key in frontend code

Run the OpenAI SDK on a server, in a command-line script, or in another trusted environment. Never commit a complete API key to Git or expose it in logs, screenshots, or browser code.

Install the SDK ​

Python ​

bash
python -m pip install --upgrade openai

TypeScript / JavaScript ​

bash
npm install openai

Migrate from a direct OpenAI connection ​

Python ​

The Python SDK uses the snake-case base_url option:

python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["LAYSO_API_KEY"],
    base_url="https://api.layso.ai/v1",
)

TypeScript / JavaScript ​

The TypeScript and JavaScript SDKs use the camel-case baseURL option:

ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.LAYSO_API_KEY,
  baseURL: "https://api.layso.ai/v1",
});

The option names are different

Python uses base_url; TypeScript and JavaScript use baseURL. The Base URL already contains /v1, so do not append it again.

Set the API key ​

macOS / Linux / WSL ​

bash
export LAYSO_API_KEY="<YOUR_LAYSO_API_KEY>"

Windows PowerShell ​

powershell
$env:LAYSO_API_KEY = "<YOUR_LAYSO_API_KEY>"

Windows CMD ​

bat
set LAYSO_API_KEY=<YOUR_LAYSO_API_KEY>

These commands affect only the current terminal session. For production, store the key with your deployment platform's secret manager.

Use the Responses API ​

The Responses API is the recommended choice for new projects. The selected model must support this endpoint; verify its capabilities in the LAYSO AI API reference and model details.

Python ​

python
response = client.responses.create(
    model="<MODEL_ID>",
    input="Introduce LAYSO AI in one sentence.",
)

print(response.output_text)

TypeScript / JavaScript ​

ts
const response = await client.responses.create({
  model: "<MODEL_ID>",
  input: "Introduce LAYSO AI in one sentence.",
});

console.log(response.output_text);

Use Chat Completions ​

Existing projects can continue using Chat Completions. The message structure remains compatible with the OpenAI SDK.

Python ​

python
completion = client.chat.completions.create(
    model="<MODEL_ID>",
    messages=[
        {"role": "user", "content": "Introduce LAYSO AI in one sentence."}
    ],
)

print(completion.choices[0].message.content)

TypeScript / JavaScript ​

ts
const completion = await client.chat.completions.create({
  model: "<MODEL_ID>",
  messages: [
    { role: "user", content: "Introduce LAYSO AI in one sentence." },
  ],
});

console.log(completion.choices[0].message.content);

Stream output ​

Python ​

python
stream = client.chat.completions.create(
    model="<MODEL_ID>",
    messages=[{"role": "user", "content": "Write a short welcome message."}],
    stream=True,
)

for chunk in stream:
    text = chunk.choices[0].delta.content or ""
    print(text, end="", flush=True)

TypeScript / JavaScript ​

ts
const stream = await client.chat.completions.create({
  model: "<MODEL_ID>",
  messages: [{ role: "user", content: "Write a short welcome message." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Verify the migration ​

After a minimal request succeeds, confirm the following:

  1. The response model matches the model selected on the Models and pricing page.
  2. The successful request has an itemized charge in LAYSO AI task or billing details.
  3. A failed request did not create a charge.
  4. The project no longer contains the direct OpenAI URL or an old key.

Compatibility scope ​

LAYSO AI's OpenAI-compatible layer covers common call patterns, but exact capabilities depend on the endpoint, model, and provider. Do not assume that every OpenAI-specific option works with every model.

CapabilityGuidance
ResponsesPrefer for new projects; confirm model support first
Chat CompletionsSuitable for migrating existing chat projects
StreamingSupported by both call patterns; follow the matching API reference
Function calling / JSON / VisionCheck the selected model's capability details
Embeddings / ImagesUse the matching endpoint and a capable model
ModelsQuery through /v1/models

Frameworks and tools ​

Any server-side framework that accepts a custom OpenAI Base URL and API key can follow the same migration pattern. This documentation includes dedicated guides for:

When configuring another framework, use the option names supported by its current version. Do not assume they are identical to the OpenAI SDK's base_url or baseURL names.

Troubleshooting ​

401 or 403 ​

Confirm the key came from the LAYSO AI API Keys page, contains no whitespace or line breaks, and has been reloaded by the process.

404 ​

The client Base URL must be https://api.layso.ai/v1. Do not use only https://api.layso.ai, and do not duplicate the path as /v1/v1.

Model not found ​

Do not guess a model alias. Return to the Models and pricing page, copy the exact model ID, and retry.

An option is rejected ​

Provider and model capabilities differ. Remove optional parameters, then compare the request with the current LAYSO AI API reference.

Browser CORS or exposed-key warning ​

Do not call the API directly from browser code. Run the OpenAI SDK on your server and expose only a controlled backend route to the frontend.

Further reading ​

LAYSO AI API 文档