> ## Documentation Index
> Fetch the complete documentation index at: https://docs.4096bytes.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Codex SDK

> Connect 4096bytes through Codex SDK in application code

Codex SDK is for integrating Codex into your own app, internal tool, or automation workflow. Copy the Base URL, API Key, and model name from the dashboard, then pass the connection values when you instantiate `Codex`.

<Tip>
  Use the Base URL, API Key, and model name shown in the dashboard. Do not copy these values from screenshots, old docs, or another user's config.
</Tip>

## Prerequisites

* You have completed [Create an account](/en/quickstart/create-account).
* You have completed [Get an API Key](/en/quickstart/get-api-key).
* Node.js 18 or later is installed.
* Codex CLI is installed and `codex --version` works.
* You have a TypeScript or JavaScript project.

<Warning>
  An API Key is a sensitive credential. Do not put it in frontend code, commit it to a repository, or print it in logs.
</Warning>

## Use cases

Codex SDK is useful when you need to place Codex inside an existing engineering workflow.

| Scenario                     | What it is good for                                                                                                           |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| CI/CD automation             | Trigger code checks, root-cause analysis, fix suggestions, or structured reports in a pipeline.                               |
| Automated code review        | Run review tasks in GitHub Actions, GitLab CI/CD, Azure DevOps Pipelines, or Jenkins, then publish findings as code comments. |
| Internal developer tools     | Add Codex to internal platforms for migration plans, repository analysis, or build failure diagnosis.                         |
| In-app engineering assistant | Start Codex threads from your application and let Codex handle multi-step engineering tasks.                                  |
| Structured output workflows  | Ask Codex for JSON schema output, then call your SCM, Slack, ticketing system, or internal APIs.                              |

If you only want to use 4096bytes from a local terminal, start with [Codex CLI](/en/clients/codex). Use the SDK when you need to control Codex from code.

## Install the SDK

Install Codex SDK in your project.

```bash theme={null}
npm install @openai/codex-sdk
```

Codex SDK calls the local Codex CLI. If Codex CLI is not installed, install it first:

```bash theme={null}
npm install -g @openai/codex
```

After installation, verify the command:

```bash theme={null}
codex --version
```

If you use pnpm or yarn, use the matching command.

```bash theme={null}
pnpm add @openai/codex-sdk
```

```bash theme={null}
yarn add @openai/codex-sdk
```

## Copy connection values

<Steps>
  <Step title="Open API Keys">
    Open the dashboard and go to **API Keys**.
  </Step>

  <Step title="Choose an API Key">
    Find the API Key you want to use with the SDK, then click **Use key**.
  </Step>

  <Step title="Copy SDK values">
    Copy the Base URL, API Key, and model name shown in the dashboard. Replace every placeholder in the example with the live dashboard value.
  </Step>
</Steps>

## Configure environment variables

Store the API Key, Base URL, and model name in server-side environment variables.

```bash theme={null}
export CODEX_API_KEY="YOUR_4096BYTES_API_KEY"
export CODEX_BASE_URL="YOUR_BASE_URL"
export CODEX_MODEL="YOUR_MODEL_NAME"
```

If your runtime uses a `.env` file, write:

```bash theme={null}
CODEX_API_KEY=YOUR_4096BYTES_API_KEY
CODEX_BASE_URL=YOUR_BASE_URL
CODEX_MODEL=YOUR_MODEL_NAME
```

## Initialize Codex

Create a `Codex` instance in server-side code. Pass `apiKey` and `baseUrl` to the constructor.

```ts theme={null}
import { Codex } from "@openai/codex-sdk";

const apiKey = process.env.CODEX_API_KEY;
const baseUrl = process.env.CODEX_BASE_URL;
const model = process.env.CODEX_MODEL;

if (!apiKey || !baseUrl || !model) {
  throw new Error("Missing CODEX_API_KEY, CODEX_BASE_URL, or CODEX_MODEL");
}

const codex = new Codex({
  apiKey,
  baseUrl,
});

const thread = codex.startThread({
  model,
});

const result = await thread.run("Describe the purpose of this project in one sentence.");

console.log(result.finalResponse);
```

Set `CODEX_API_KEY` to the API Key you created in the dashboard. Copy `CODEX_BASE_URL` and `CODEX_MODEL` from the dashboard.

## Verify the connection

Start with a read-only prompt.

```ts theme={null}
const result = await thread.run("Reply with ok only. Do not modify any files.");
```

After the response works, connect the SDK to your real workflow.

## Code review example

OpenAI's official cookbook shows an automated code review workflow. The workflow triggers Codex in a CI/CD runner, has Codex read the PR diff, asks for structured JSON schema output, and then calls the SCM API to publish inline comments.

You can use this pattern for:

* Automated PR review.
* Review workflows for on-premise repositories or non-GitHub SCMs.
* Creating tickets, sending Slack notifications, or updating internal quality dashboards from structured results.

Start with read-only review tasks. After the output is stable, connect comment publishing, ticket creation, or other actions that change external state.

Official example: [Build Code Review with the Codex SDK](https://developers.openai.com/cookbook/examples/codex/build_code_review_with_codex_sdk).

For more SDK usage, see the official OpenAI docs: [Codex SDK](https://developers.openai.com/codex/sdk).

## Common issues

<AccordionGroup>
  <Accordion title="Which Base URL should I use">
    Use the current Base URL shown in the dashboard. Do not copy a fixed address from docs, screenshots, or old config.
  </Accordion>

  <Accordion title="Which model name should I use">
    Use the model name shown in the dashboard. Copy the full value, including casing, hyphens, and version suffixes.
  </Accordion>

  <Accordion title="Can I call it from browser frontend code">
    This is not recommended. Frontend code exposes the API Key. Call the SDK from server-side code, backend jobs, or controlled automation.
  </Accordion>

  <Accordion title="Authentication failed">
    Check that the API Key was copied completely, the runtime can read the environment variables, and the Base URL belongs to the same dashboard account or environment.
  </Accordion>
</AccordionGroup>
