API Development with Codex
You need to build a new API for managing team invitations. It requires five endpoints, input validation, auth middleware, rate limiting, database queries, email notifications, and comprehensive tests. The spec says “two days.” You could grind through it sequentially. Or you could plan the API in the IDE, scaffold the routes in a local thread, write tests in a parallel worktree, and have Codex generate the OpenAPI spec while you review the business logic.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A parallel API development workflow: scaffold routes, write tests, and generate docs simultaneously
- Prompts for building endpoints with proper validation, error handling, and middleware
- Techniques for testing APIs in the Codex CLI with inline command execution
- A pattern for keeping OpenAPI specs in sync with implementation using automations
The Workflow
Section titled “The Workflow”Step 1: Design the API Contract First
Section titled “Step 1: Design the API Contract First”Start in the IDE extension or Codex App with a design prompt. Getting the API shape right before writing code prevents costly rework.
Review the spec and iterate before implementation. This becomes your contract.
Step 2: Scaffold Routes in a Local Thread
Section titled “Step 2: Scaffold Routes in a Local Thread”With the API contract defined, scaffold the actual endpoint handlers. Use Local mode for the initial scaffolding since you want the files in your working directory.
After Codex generates the files, review in the App’s review pane. Stage and commit the scaffolding before moving to parallel work.
Step 3: Write Tests in a Parallel Worktree
Section titled “Step 3: Write Tests in a Parallel Worktree”Create a Worktree thread based on the branch with your scaffolded routes. The test agent runs in complete isolation — it can run the test suite repeatedly without interfering with your local development.
Step 4: Test Endpoints Interactively in the CLI
Section titled “Step 4: Test Endpoints Interactively in the CLI”The CLI is excellent for rapid API testing because you can execute commands inline. Start Codex with your dev server running in another terminal:
codexThen test your endpoints interactively:
The CLI executes curl commands directly and shows you the responses. This interactive testing catches issues that unit tests miss — middleware ordering, header requirements, response format inconsistencies.
Step 5: Generate OpenAPI Spec from Implementation
Section titled “Step 5: Generate OpenAPI Spec from Implementation”After the API is tested and working, generate (or update) the OpenAPI spec from the actual implementation:
Set up an automation to keep the spec in sync:
When This Breaks
Section titled “When This Breaks”Zod schemas and database schema drift apart. If you define Zod validation schemas separately from your Drizzle ORM schema, they can get out of sync. Tell Codex: “Derive Zod schemas from the Drizzle schema types where possible. Do not duplicate type definitions.”
Tests pass locally but fail in the worktree. This happens when the worktree’s database was not set up. Ensure your local environment setup script runs database migrations. Check the integrated terminal (Cmd + J) for setup script errors.
Rate limiting tests are flaky. Rate limit tests that depend on timing are inherently flaky. Tell Codex: “For rate limiting tests, send requests in a tight loop without delays, and verify the status code switches from 200 to 429 after the configured limit. Do not rely on time-based windows in tests.”
The generated OpenAPI spec does not match a specific consumer’s expectations. Codex generates the spec from your code, but your API consumers may expect different field names or response shapes. After generating, review the spec against your actual consumer requirements. Include consumer-specific constraints in your AGENTS.md review guidelines.