If you want to invoke OpenAI’s Codex coding agent from your own software, use the Codex SDK for local repository workflows or the managed Agents API for an agent running in an OpenAI-managed environment. If you only need a model to return generated code, the Responses API is usually simpler. These interfaces solve different problems; there is no single universal Codex REST endpoint for all of them.
The examples below use the documented interfaces available on December 4, 2026. They are small read-only repository analysis examples, not a claim that an authenticated or billed agent run was performed.
To estimate the cost of your Codex subscription or API-key workload, see our OpenAI Codex pricing guide.
For a comparison of tools across coding, testing and review, see our AI developer tools guide.
If you are just getting started and do not yet need a programmatic interface, follow our OpenAI Codex beginner guide.
If you are deciding between conversational help and repository-based implementation, read our Codex and ChatGPT comparison.
Which Codex API should you use?
| Interface | Where it runs | Use it when |
|---|---|---|
| Codex SDK | Local agent runtime controlled by your application | You need repository analysis or coding workflows in scripts or CI |
| Codex app server | Local Codex process with a richer client protocol | You need interactive approvals, streamed events and conversation management |
| Agents API (beta) | Managed Codex harness; supported hosted or self-hosted environments | You need application-level agent sessions without implementing the harness yourself |
| Responses API | OpenAI model API | You need code generation or a model integrated with your own tools |
| codex exec | Non-interactive CLI process | You want a single scripted task without adding SDK integration |
Codex is the agent that coordinates repository context, tools and model calls. A coding model alone does not automatically inspect your local repository or run its tests. The TypeScript SDK wraps the Codex CLI; the Python SDK communicates with the local app server. The managed Agents API exposes a different session-oriented interface. For interactive IDE work rather than programmatic integration, see our Codex in VS Code setup guide.
How to get and use an API key for Codex
For usage-based API access, create a project in the OpenAI Platform, configure billing and generate a project API key with the permissions your integration requires. Store the key in a secret manager or an environment variable supplied by your deployment system. Our OpenAI API platform guide explains project and credential setup.
Codex local clients also support ChatGPT sign-in for subscription-based access. Authentication determines the applicable billing, workspace controls and feature availability. The official authentication documentation recommends API keys for programmatic CLI jobs. To authenticate an installed CLI using an environment variable, run:
printenv OPENAI_API_KEY | codex login --with-api-key
Creating an API key does not grant free model usage. API-authenticated sessions are billed under the Platform account rather than your ChatGPT plan allowance. Never paste credentials into a prompt, commit them to a repository or distribute them in browser-side JavaScript. In CI, use the runner’s secure secret store and prevent untrusted pull requests from accessing it.
Run Codex from TypeScript
The official TypeScript SDK requires a server-side Node.js runtime (the documented minimum is version 18). Use a currently supported Node.js release and install the package inside a trusted development project:
npm install @openai/codex-sdk
Start in a small Git repository containing non-sensitive files. After configuring authentication, create a file named review.mjs with the following example. Run it from the repository root.
import { Codex } from "@openai/codex-sdk";
const codex = new Codex();
const thread = codex.startThread({
workingDirectory: process.cwd(),
sandboxMode: "read-only",
approvalPolicy: "never",
networkAccessEnabled: false,
});
const result = await thread.run(
"Inspect the repository and identify one missing test. " +
"Explain the proposed test without changing files."
);
console.log(result.finalResponse);
console.log("Usage:", result.usage);
node review.mjs
The agent returns a summary and can report usage and tool activity. The read-only policy and disabled network access reduce the scope of local tool operations; they do not eliminate the model service connection or every possible risk. Keep sensitive files outside the workspace. Use a new thread for unrelated tasks and retain a thread ID only when a later authorized step genuinely needs the original context.
For real-time progress, the TypeScript SDK exposes runStreamed() and typed events. Use those events to show work status or record an audit trail, rather than assuming that a successful final text response proves every intermediate operation succeeded.
Use the Python Codex SDK
The official Python SDK is available as a stable release and requires Python 3.10 or newer. Its published packages include a pinned Codex runtime dependency. Create a virtual environment and install the SDK:
python -m pip install openai-codex
Run the following example in a trusted test repository with the intended account configured. It deliberately omits a model ID so the configured default can be used.
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(
sandbox=Sandbox.read_only,
)
result = thread.run(
"Summarize the project's test structure. "
"Do not change any files."
)
print(result.final_response)
The Python SDK also provides an asynchronous client for applications with an event loop. Before using either SDK in production, pin a tested package version, check current authentication and permission settings, and verify the example against the installed runtime. These snippets were reviewed against documentation, not executed with a live API account.
When to use the managed Agents API
The managed Agents API is a beta session-based interface for OpenAI’s Codex harness. Instead of starting a local CLI subprocess, your application configures a session with instructions, available tools and an environment. OpenAI can provision a hosted sandbox; the documentation also describes supported self-hosted environment configurations.
The documented session creation endpoint is POST /v1/agents/sessions, with the OpenAI-Beta header set to agents=v1. Check the official quickstart for the current supported SDK, model access, project permissions and required environment settings before integrating it. After creating a session, send a task, consume progress events or webhooks, and submit a later instruction if the session needs to continue.
A concrete integration described in the official Agents API examples is a GitHub issue investigator that reproduces reported bugs and reports its findings. For a similar workflow, grant read access to a disposable repository copy, define a narrowly scoped test, and require separate human approval before writing to the actual issue or repository. The managed harness is not a substitute for your application’s authorization layer.
Production integration and cost controls
Control permissions and third-party APIs
When connecting an external tool, define its allowed operations separately from the agent prompt. For example, an issue tracker integration might allow reading an issue but prohibit updating it without explicit approval. A custom model gateway or compatible third-party endpoint is a separate configuration choice; verify the supported protocol, authentication and tool behavior before assuming the Codex runtime can use it. Do not give an external service your OpenAI credential unless its documented authentication flow specifically requires that credential and you trust the destination.
Measure the full cost of a job
An agent may make multiple model calls while reading files, planning and checking results. Budget for input, cached input, output and any additional applicable tool or environment charges. For example, if a job reports 30,000 input tokens, 10,000 cached input tokens included in that total and 4,000 output tokens, calculate the cost using 20,000 uncached input tokens, 10,000 cached input tokens and 4,000 output tokens at the model’s current applicable rates. Do not count cached input twice. See our OpenAI API cost guide for the pricing calculation.
Test an integration before allowing edits
Use the same clean test repository for each run. Check whether the agent correctly identifies a known missing test, respects read-only access, avoids sensitive information and reports unsupported requests accurately. Record the package version, model, permission profile, duration and actual usage. Apply timeouts and concurrency limits. If you later allow code changes, run independent tests and review the diff before merging. Promptfoo’s documented Codex SDK integration is an example of evaluating final text, tool activity and usage together.
Frequently asked questions
Is there a free Codex API key?
A key is a credential, not an allowance of free requests. Any promotional credit or subscription access depends on your account and the authentication method.
Can I use a Codex API key in the CLI?
Yes. The official CLI supports API-key login through standard input. Usage-based billing applies when you authenticate with an API key.
Is the Codex SDK the same as the OpenAI Agents SDK?
No. The Codex SDK controls the Codex agent. The Agents SDK is a library for developing agent applications, and the managed Agents API is a separate hosted harness interface.
Does Codex have to use a third-party API?
No. You can use supported OpenAI models and tools directly. External APIs are optional integrations that should be granted only the permissions needed for the task.
Can I call a model directly to generate code?
Yes. The Responses API is suitable when you need model-generated code rather than a repository-aware agent that can perform tool operations.

