OapiCodemode

OpenAPI search-and-execute for LLM agents, in Elixir — Cloudflare codemode style, but the sandbox never sees your credentials.

Drop in one or more OpenAPI specs and get two tools for your agent's tool loop:

Because the request never actually executes inside the sandbox, the sandbox never holds an API key, bearer token, or any other secret — credentials are attached host-side, after the JS has finished running.

Installation

def deps do
[
{:oapi_codemode, "~> 0.1.0"},
# plus the engine for your chosen executor — for the recommended
# Executor.SafeJS (QuickJS-NG NIF, precompiled binaries):
{:ex_safejs, "~> 0.3.1"}
]
end

Executor.Deno needs no extra dep, just a deno 2.x binary on PATH. See Executor status for the trade-offs.

Specs can be huge, and dumping every operation into the system prompt wastes context and drowns the model in noise. Instead, the spec is handed to the model as data it can query with code: filter by tag, grep summaries, pull out just the operations it needs. This is the same idea behind Cloudflare's codemode — let the model write code against tools instead of chaining tool calls one at a time — applied to OpenAPI specs specifically.

Quickstart

# 1. Start a registry (usually under your app's supervision tree).
{:ok, registry} = OapiCodemode.Registry.start_link(name: nil)
# 2. Ingest a spec and register it with the config the spec itself can't
# know (base URL, which security scheme to use, per-tenant context).
:ok =
OapiCodemode.ingest_and_register(
registry,
"petstore",
File.read!("petstore.json"),
base_url: "https://api.petstore.example.com/v1"
)
# The API name becomes a JavaScript identifier inside the sandbox
# (`apis.petstore`, `specs.petstore`, `context.petstore`), so it must match
# /^[A-Za-z_][A-Za-z0-9_]*$/ — "my-api" and "2fast" are rejected with
# {:error, {:invalid_api_name, name}}.
# 3. Get the tool definitions.
tools =
OapiCodemode.tools(
registry: registry,
executor: OapiCodemode.Executor.Mock,
resolver: MyApp.CredentialResolver,
policy: :read_only
)

Each entry in tools looks like:

%{
name: "search_apis" | "execute_api_code",
description: "...",
input_schema: %{...},
handler: fn args, host_ctx -> {:ok, json} | {:error, message} end
}

Wire the handlers into your host's tool loop — whatever calls tools by name and feeds results back to the model:

Enum.find(tools, &(&1.name == tool_name)).handler.(
tool_args,
%{context: %{user_id: current_user.id}}
)

host_ctx.context is opaque to the library — it's handed straight to your credential resolver so it can look up the right token for whoever is making the call.

Caching a parsed spec across registrations

ingest_and_register/4 parses and registers in one call — fine for a boot-time registry that ingests each spec once. Hosts that build a registry more often than that (per loop start, per request, ...) should parse once and cache the result: OapiCodemode.ingest/1 is the pure ingest step, and OapiCodemode.register/4 registers an already-ingested %Artifact{} without re-parsing it.

{:ok, artifact} = OapiCodemode.ingest(File.read!("petstore.json"))
# ... stash `artifact` in your own cache, keyed however you invalidate it ...
:ok = OapiCodemode.register(registry, "petstore", artifact, base_url: "https://api.petstore.example.com/v1")

register/4 takes the same config options as ingest_and_register/4 (base_url, security_scheme, sandbox_globals, req_options, validate, max_response_bytes) and returns {:error, {:invalid_config_option, key}} for an unrecognized one, same as ingest_and_register/4.

Credential resolver

Implement the OapiCodemode.Credentials behaviour to tell the library what credential to use for a given API and caller; the library figures out how to attach it from the spec's securityScheme.

defmodule MyApp.CredentialResolver do
@behaviour OapiCodemode.Credentials
@impl true
def resolve("petstore", _security_scheme, _request, %{user_id: user_id}) do
{:ok, {:bearer, MyApp.Tokens.fetch!(user_id, :petstore)}}
end
def resolve(_api_name, _security_scheme, _request, _context) do
{:ok, :none}
end
end

resolve/4 returns {:ok, {:bearer, token}}, {:ok, {:basic, user, pass}}, {:ok, {:api_key, value}}, {:ok, :none}, or {:error, reason}. The credential value is attached to the outgoing request by OapiCodemode.Credentials.attach/2 and never crosses into the sandbox or gets logged in a tool call transcript.

The third argument, request, is the resolved destination — %{method:, base_url:, host:, path:} — computed before credential attachment, so a resolver can enforce a spend-time allowlist (exact host, https-only, ...) at the same choke point it resolves credentials, not just at registration time. path is the OpenAPI path template (unsubstituted); the full wire path is base_url's path prefix, if any, plus the substituted path.

Error contract: return a binary {:error, message} and it crosses to the sandbox/model verbatim — never put a credential or other secret in that string. Return any non-binary reason ({:error, {:expired, token}}, {:error, :not_found}, ...) and the library logs it in full via Logger but replaces it with a fixed, redacted string before it reaches the model.

Registration options

ingest_and_register/4 and register/4 take the same ApiConfig options:

Custom tool names

OapiCodemode.tools/1 accepts :search_tool_name (default "search_apis") and :execute_tool_name (default "execute_api_code") to rename the emitted tools — useful when a host runs one registry per API instance and wants per-instance tool names instead of one shared pair:

OapiCodemode.tools(registry: reg, executor: OapiCodemode.Executor.Deno,
resolver: MyApp.CredentialResolver, policy: :read_only,
search_tool_name: "petstore_api_search", execute_tool_name: "petstore_api_execute")

Executor status

The sandbox that runs the LLM-written JS sits behind the OapiCodemode.Executor behaviour:

The Executor behaviour contract is stable; swapping executors doesn't change how you call OapiCodemode.tools/1.

Design rationale

See docs/plans/2026-08-16-openapi-search-execute-design.md for the full design writeup — why search and execute are separate tools, why validation and credentialing live in Elixir rather than the sandbox, and how the registry, ingest pipeline, and proxy fit together.