NEAR AI Cloud: OpenAI Compatibility
How to use NEAR AI Cloud with standard OpenAI SDKs
NEAR AI Cloud implements the OpenAI-compatible inference surfaces documented below. You can use the official OpenAI client libraries for Python and JavaScript/TypeScript by changing the base URL and API key. Features that require provider-side storage or tool execution are intentionally limited; each section describes the supported contract.
Configuration
To use the OpenAI SDKs with NEAR AI Cloud, you simply need to override the base_url and provide your NEAR AI api_key.
Base URL
Use the NEAR AI Cloud Gateway base URL:
https://cloud-api.near.ai/v1
API Key
You can generate your API key in the NEAR AI Cloud Dashboard.
Request IDs
Gateway responses from https://cloud-api.near.ai/v1 include an X-Request-Id response header. Support may ask you for this value when debugging a request.
X-Request-Id is opaque support/debugging metadata; it is not W3C traceparent or distributed trace context. If you send your own X-Request-Id, use a non-sensitive UUID value. X-Request-Id values must not contain secrets or PII. X-Org-Id and X-Workspace-Id are internal tenant headers; public clients cannot set or override them.
Usage Examples
Python
Python SDK
First, install the OpenAI Python library:
pip install openai
Then, initialize the client with NEAR AI configuration:
from openai import OpenAI
client = OpenAI(
base_url="https://cloud-api.near.ai/v1",
api_key="YOUR_NEAR_AI_API_KEY",
)
response = client.chat.completions.create(
model="zai-org/GLM-5.1-FP8",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
temperature=0.7,
max_tokens=500
)
print(response.choices[0].message.content)
Async Usage
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://cloud-api.near.ai/v1",
api_key="YOUR_NEAR_AI_API_KEY",
)
async def main():
stream = await client.chat.completions.create(
model="zai-org/GLM-5.1-FP8",
messages=[{"role": "user", "content": "Tell me a story about space."}],
stream=True,
)
async for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
asyncio.run(main())
JavaScript/TypeScript
Node.js / TypeScript SDK
First, install the OpenAI Node.js library:
npm install openai
Then, initialize the client with NEAR AI configuration:
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://cloud-api.near.ai/v1',
apiKey: 'YOUR_NEAR_AI_API_KEY',
});
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
model: 'zai-org/GLM-5.1-FP8',
});
console.log(completion.choices[0].message.content);
}
main();
Streaming Example
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://cloud-api.near.ai/v1',
apiKey: 'YOUR_NEAR_AI_API_KEY',
});
async function main() {
const stream = await openai.chat.completions.create({
model: 'zai-org/GLM-5.1-FP8',
messages: [{ role: 'user', content: 'Write a haiku about coding.' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
}
main();
Supported Features
NEAR AI Cloud supports the following core OpenAI API features:
- Chat Completions (
/v1/chat/completions) - Including tool calling, structured outputs, and reasoning - Completions (
/v1/completions) - Legacy text completions - Models List (
/v1/models) - Embeddings (
/v1/embeddings) - See Beyond Chat - Images (
/v1/images/generations,/v1/images/edits) - See Beyond Chat - Audio Transcription (
/v1/audio/transcriptions) - See Beyond Chat - Responses (
/v1/responses) - Stateless,store: falseinference with client-managed history and custom functions; see Stateless Responses - Streaming (Server-Sent Events)
NEAR AI Cloud also provides non-OpenAI extensions:
- Reranking (
/v1/rerank) - Score documents against a query - Privacy classification (
/v1/privacy/classify) - Detect personally identifiable information through the gateway - Privacy redaction (
/v1/privacy/redact) - Gateway-only endpoint for redacting personally identifiable information - Attestation & Signatures (
/v1/attestation/report,/v1/signature/{chat_id}) - See Verification
Stateless Responses
The Responses API preserves the OpenAI Responses input, output, and streaming shapes without storing conversation or response history. Set store: false or omit it, and include any prior context in each new request.
Python
from openai import OpenAI
client = OpenAI(
base_url="https://cloud-api.near.ai/v1",
api_key="YOUR_NEAR_AI_API_KEY",
)
response = client.responses.create(
model="z-ai/glm-5.2",
input="Explain confidential inference in one sentence.",
store=False,
)
print(response.output_text)
JavaScript/TypeScript
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://cloud-api.near.ai/v1',
apiKey: 'YOUR_NEAR_AI_API_KEY',
});
async function main() {
const response = await openai.responses.create({
model: 'z-ai/glm-5.2',
input: 'Explain confidential inference in one sentence.',
store: false,
});
console.log(response.output_text);
}
main();
See Stateless Responses for caller-managed multi-turn history, custom function replay, retired endpoints, and unsupported server-executed tools.
Environment Variables
Alternatively, you can set the environment variables to avoid hardcoding credentials in your code:
export OPENAI_BASE_URL="https://cloud-api.near.ai/v1"
export OPENAI_API_KEY="your-near-ai-key"
Then you can initialize the client without arguments:
# Python
client = OpenAI()
// JavaScript
const openai = new OpenAI();