OpenAI codex api: Quickstart with Python
This guide shows how to use the uncensored LLM with standard OpenAI SDKs via our openai compatible API. You can start sending requests immediately by updating your base URL and API key.
Install the Official SDK
Use the official SDK to ensure compatibility with our openai proxy API. This approach works with any standard library that supports the OpenAI protocol.
- Install the package via your preferred package manager.
- Verify the installation by importing the library.
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);
Set Your Base URL and Key
Point your client to our openai gateway endpoint to access the uncensored AI model. Replace the default base URL with our specific endpoint.
- Set the API key in your environment variables.
- Configure the base URL to
https://api.openaicodexapi.com/v1.
This setup ensures your requests route correctly to our servers without modifying your existing code structure.
curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'
Send Your First Request
Send a simple text completion to test the connection. Use the model ID uncensored for the request.
- Define the message content.
- Execute the request.
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);The response returns the generated text directly.
Enable Streaming and JSON Mode
Stream responses for lower latency or enforce JSON mode for structured data. Set stream=true for real-time token delivery.
- Use
response_formatfor JSON output. - Handle chunks in your code.
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'Streaming helps you display partial results quickly.
Handle Tools and Function Calling
Define tools in your request to enable function calling. Specify the tool definitions and let the model decide when to use them.
- Define the function schema.
- Parse the tool calls in the response.
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);This feature allows integration with external systems.
Limits, Errors, and Context Window
Monitor your usage to avoid rate limits or errors. The context window supports 100,000 tokens total.
- Handle 401 errors for invalid keys.
- Handle 402 errors for insufficient credit.
- Handle 429 errors for rate limits.
curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)FAQ
What is the openai codex API?
It is an uncensored LLM API that is compatible with the OpenAI protocol. You can use it with standard SDKs by updating the base URL.
How do I handle errors?
Common errors include 401 for invalid keys, 402 for no credit, and 429 for rate limits. Check your account balance and key validity.
What is the context window size?
The context window is 100,000 tokens for both prompt and completion combined. Max output is 16,000 tokens per request.
Get your API key
Register, copy the key, and update the base URL. That is all.