Cookbook
Worked recipes for the jobs that come up most. Each one is self-contained;
copy the parts you need. Recipes that use the fake API from apic demo can
be run as they stand.
Running against real APIs
Section titled “Running against real APIs”examples/ in the repo has three runnable projects, each targeting a real
public API instead of apic demo’s in-process fake:
apic run auth.http users.http -C examples/httpbin --env dev # basic + bearer, no account neededapic run repo.http -C examples/github --env dev # bearer, needs your own PATapic run search.http -C examples/spotify --env dev # oauth2 client_credentials, needs your own appFor github and spotify, drop your own credentials into that project’s
http-client.private.env.json first; see examples/README.md.
Log in once and reuse the token everywhere
Section titled “Log in once and reuse the token everywhere”The pattern apic is built around: one request captures the token, every other request uses it, and the value survives between invocations.
### Log in# @name login# @assert status == 200# @capture token = body.$.access_tokenPOST {{baseUrl}}/auth/loginContent-Type: application/json
{"user": "{{user}}", "password": "{{password}}"}
### Anything else# @name whoami# @assert status == 200GET {{baseUrl}}/meAuthorization: Bearer {{token}}apic run login # onceapic run whoami # now, tomorrow, in another shellThe token lands in .apic/session.json under the current environment.
apic session shows it, apic session clear forgets it, and --no-session
ignores it for one run. If you forget the login, the error says which
request would have captured the missing value.
To not have to remember it, let the request say what it needs:
### Anything else# @name whoami# @ref login# @assert status == 200GET {{baseUrl}}/meAuthorization: Bearer {{token}}Now apic run whoami logs in first when {{token}} is missing and goes
straight to /me when it is not. # @forceRef login logs in every time,
for a token that must not be reused. See
format.md.
Create, read, update, delete in one flow
Section titled “Create, read, update, delete in one flow”Requests in a file run in order and pass values down the chain, so a full resource lifecycle is one command.
### Create a todo# @name create-todo# @assert status == 201# @capture todoId = body.$.idPOST {{baseUrl}}/todosAuthorization: Bearer {{token}}Content-Type: application/json
{"title": "Write docs"}
### Read it back# @name get-todo# @assert status == 200# @assert body.$.title == "Write docs"GET {{baseUrl}}/todos/{{todoId}}Authorization: Bearer {{token}}
### Mark it done# @name update-todo# @assert status == 200# @assert body.$.done == truePUT {{baseUrl}}/todos/{{todoId}}Authorization: Bearer {{token}}Content-Type: application/json
{"done": true}
### Delete it# @name delete-todo# @assert status == 204DELETE {{baseUrl}}/todos/{{todoId}}Authorization: Bearer {{token}}apic run todos.http # stops at the first failureapic run todos.http --keep-going # runs everything, then reportsA longer version of this file ships with apic demo:
apic run login todos.http -C apic-demo --keep-going runs it offline. Its
not-found request fails on purpose, to show what a failure looks like.
A smoke test in CI that never leaks secrets
Section titled “A smoke test in CI that never leaks secrets”- uses: hungovercoders/apic/setup-apic@v0- run: apic validate -C api --format github- run: apic run auth.http smoke.http -C api --env staging --json --redact env: APIC_VAR_password: ${{ secrets.API_PASSWORD }}setup-apicinstalls a release verified againstchecksums.txt, cached per version and platform, on every runner OS;with: version: v0.2.0pins one. Elsewhere,install.shdoes the same.APIC_VAR_<name>overrides any variable from the shell, so no secret needs to reach the runner’s disk.--redactmasks header values, bodies, query values and captured values in the stored log. Sensitive headers are masked even without it.--jsongives one object per request for whatever reads the log next.- The exit code fails the step: 1 for a failed assertion, 3 for a network error.
For a report a person opens rather than a log a tool parses, add
--report (or apic test --format html --output) and upload the file as
an artifact; it is one HTML file with no external assets, redacted the
same way:
- run: apic run auth.http smoke.http -C api --env staging --redact --report smoke.html env: APIC_VAR_password: ${{ secrets.API_PASSWORD }}- uses: actions/upload-artifact@v7 if: always() with: name: smoke-report path: smoke.htmlapic validate on its own is a cheap pull-request check: it parses every
file, reports duplicate names, bad selectors, unknown auth options and
missing body files, and exits 2 if anything is an error. With
--format github each finding becomes an annotation on the pull request
at the exact line and column:
- run: apic validate -C api --format githubIn a container
Section titled “In a container”ghcr.io/hungovercoders/apic is the same binary on scratch, with the CA
bundle it needs for TLS: about 16 MB (about 7 MB to pull), for amd64 and
arm64, tagged with the version (0.2.0 and v0.2.0), the minor (v0.2)
and latest. Mount the
project at /work and pass secrets as APIC_VAR_* variables:
docker run --rm -v "$PWD/api:/work" -e APIC_VAR_password \ ghcr.io/hungovercoders/apic run auth.http smoke.http --env staging --json --redact- Files apic writes (
.apic/session.json, a report) belong to root unless you add--user "$(id -u):$(id -g)". apic uineeds a terminal:docker run --rm -it ….apic mcp --http 0.0.0.0:8765with-p 8765:8765serves an agent outside the container (set a token, as for any non-loopback address).apic demolistens on the container’s loopback only, so it is for the container’s own use.
The image has no shell, so it cannot be a CI system’s job image where the
job runs a script inside it (GitLab’s image:, an Azure container job).
There, copy the binary into the image you already use:
FROM node:22COPY --from=ghcr.io/hungovercoders/apic:v0.2 /apic /usr/local/bin/apicor run the installer in the job:
smoke: image: alpine:3.22 script: - apk add --no-cache curl - curl -fsSL https://raw.githubusercontent.com/hungovercoders/apic/main/install.sh | sh - apic run auth.http smoke.http -C api --env staging --json --redactThe image is signed like the release archives; verifying.md has the command.
AWS API Gateway with SigV4
Section titled “AWS API Gateway with SigV4”### List orders from a private API# @name list-orders# @auth aws service=execute-api region=eu-west-2# @assert status == 200GET https://abc123.execute-api.eu-west-2.amazonaws.com/prod/ordersAccept: application/jsonapic signs with your existing credentials: environment variables, a named profile, or an SSO session through the AWS CLI. Nothing apic-specific to configure, and no AWS SDK in the binary.
aws sso login --profile prodAWS_PROFILE=prod apic run list-orders # or: # @auth aws profile=prodSet it once for a whole project in apic.yaml instead of on every request:
auth: default: aws service=execute-api region=eu-west-2Signing happens after variables are substituted, so a signed request can
still use captured values. Other services work the same way with
service=s3, service=lambda, service=es and so on.
OAuth2 client credentials
Section titled “OAuth2 client credentials”### Anything behind a machine-to-machine token# @name report# @auth oauth2 tokenUrl={{tokenUrl}} clientId={{clientId}} clientSecret={{clientSecret}} scope="api.read"# @assert status == 200GET {{baseUrl}}/reports/daily{ "prod": { "tokenUrl": "https://login.example.com/oauth2/v2.0/token", "clientId": "..." } }// http-client.private.env.json{ "prod": { "clientSecret": "..." } }The token is fetched on first use, cached in the session for the
environment, reused until a minute before it expires and then refreshed.
apic session shows what is cached and for how long. This is the same flow
for Entra ID, Okta, Auth0, Keycloak and Cognito. See
authentication for the other grants.
Sign in as yourself
Section titled “Sign in as yourself”### My repositories, as me# @name my-repos# @auth oauth2 grant=authorization_code authUrl={{authUrl}} tokenUrl={{tokenUrl}} clientId={{clientId}} scope="repo read:user"# @assert status == 200GET https://api.github.com/user/repos{ "dev": { "authUrl": "https://github.com/login/oauth/authorize", "tokenUrl": "https://github.com/login/oauth/access_token", "clientId": "Iv1..." } }Run apic run my-repos once from a terminal: a browser opens on the
provider’s sign-in page, and the token comes back through a loopback
redirect (register http://127.0.0.1:<port>/callback with the app, with
redirectPort= on the directive when the provider wants the exact port).
From then on the cached token, refreshed while its refresh token lasts,
serves --json, agents and CI alike; only a fresh sign-in needs the
terminal again.
A token from any CLI
Section titled “A token from any CLI”When the credential comes from a tool rather than a flow:
auth: allowExec: true# @auth exec gcloud auth print-identity-tokenGET {{baseUrl}}/internal/healthexec is off unless the project opts in, because a request file that runs
commands deserves a deliberate yes.
Poll until something is ready
Section titled “Poll until something is ready”Say what “ready” looks like and how long to wait for it:
### Wait for the job# @name job-status# @retry 30 2s# @assert status == 200# @assert body.$.state == doneGET {{baseUrl}}/jobs/{{jobId}}apic run job-status sends the request until both assertions pass or thirty
attempts are spent, printing a line per failed attempt, and exits 1 if the
job never gets there. retry: in apic.yaml or --retry "30 2s" applies
the same policy to every request without its own; see
format.md.
When the condition is not one assertion can express, the shell still works, with the request kept in the file where everyone can see it:
until apic run job-status --json | jq -e '.response.body.state == "done"' >/dev/null; do sleep 2doneOr assert the state and let a retry loop use the exit code:
### Job status# @name job-status# @assert body.$.state == doneGET {{baseUrl}}/jobs/{{jobId}}for i in $(seq 1 30); do apic run job-status && break; sleep 2; doneUpload a file
Section titled “Upload a file”A multipart form, the way the editors write it: the boundary in the header,
each part between delimiter lines, and a file part whose content is a
< file reference. apic sends the file’s bytes, not the text.
### Upload a report with a title# @name upload-report# @assert status == 201# @assert body.$.files[0].filename == report.pdfPOST {{baseUrl}}/uploadContent-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundaryContent-Disposition: form-data; name="title"
Quarterly report for {{user}}--WebAppBoundaryContent-Disposition: form-data; name="file"; filename="report.pdf"Content-Type: application/pdf
< ./report.pdf--WebAppBoundary--Text parts are templates, so {{user}} resolves as anywhere else. apic curl upload-report prints the same upload as --form-string and
-F file=@report.pdf options, and apic validate fails when the part file
is missing. The demo API’s POST /upload echoes the parts back, so
apic demo gives you something to try this against.
When the whole body is a file rather than a form, the reference stands alone:
### Upload a payload written by something else# @name import-batch# @assert status == 202POST {{baseUrl}}/batchesContent-Type: application/json
< ./payload.json< sends the file as it is. <@ substitutes {{variables}} inside it
first, which is handy for a fixture with an id or a timestamp in it:
<@ ./templated-payload.jsonPaths are relative to the .http file, and apic validate fails if the
file is missing.
Download a file
Section titled “Download a file”A >> line after the body saves the response body, bytes as they came,
so a CSV export or an image lands on disk intact:
### Daily report# @name daily-report# @assert status == 200# @assert header.content-type contains text/csvGET {{baseUrl}}/reports/daily.csv
>>! ./fixtures/daily.csv>> creates the file and fails if it exists; >>! overwrites. The path
is relative to the .http file and stays inside the project; the run
says ↳ saved to fixtures/daily.csv. For a one-off without editing the
file:
apic run daily-report --output /tmp/daily.csvA binary body is summarised in the output (binary body · 12 KB · image/png) rather than printed, and under --json it travels as
base64 with "body_encoding": "base64". A saved file is created 0600
when a secret (a private variable, a captured token, a credential) went
into the request, since the response may carry one back.
Follow, or do not follow, redirects
Section titled “Follow, or do not follow, redirects”Redirects are followed by default. To assert on the redirect itself:
### The redirect itself, not its destination# @name redirect-raw# @no-redirect# @assert status == 302# @assert header.location contains /status/200GET {{baseUrl}}/redirectGive one slow endpoint more time
Section titled “Give one slow endpoint more time”### A report that takes a while# @name daily-report# @timeout 90s# @assert status == 200GET {{baseUrl}}/reports/dailyA per-request # @timeout beats --timeout, which beats timeout: in
apic.yaml, which defaults to 30 seconds.
Two environments, one command
Section titled “Two environments, one command”{ "$shared": { "userId": 42 }, "dev": { "baseUrl": "https://dev.example.com" }, "staging": { "baseUrl": "https://stg.example.com" }}for env in dev staging; do apic run smoke.http --env "$env" --keep-going || echo "$env failed"doneEach environment keeps its own captured values, so a token from dev is
never sent to staging. In the terminal UI, e switches
between them.
Run one request for every row
Section titled “Run one request for every row”id,name7,alice8,bob### Update a user# @name update-user# @assert status == 200# @assert body.$.name == {{name}}PATCH {{baseUrl}}/users/{{id}}Content-Type: application/json
{"name": "{{name}}"}apic run update-user --data users.csv --keep-going --report users.htmlEach row is one iteration, its columns variables, so the assertion checks
each user against its own row. Iterations do not share captures unless
--data-share-session; see Data-driven runs.
Describe behaviour, then test it
Section titled “Describe behaviour, then test it”### Create a user# @name create-user# @step a user named {name} exists# @assert status == 201# @capture userId = body.$.idPOST {{baseUrl}}/usersContent-Type: application/json
{"name": "{{name}}"}Feature: Users Scenario: A new user can be fetched Given I am logged in And a user named "alice" exists When I run "get-user" Then the response status is 200 And the response body "$.name" is "alice"apic test # everything under features/apic test --tags "@smoke && ~@slow"apic test --format junit --output report.xml # for CIapic test --steps prints the built-in vocabulary plus the phrases this
project declares. Full guide: testing with Gherkin.
Hand the API to an agent
Section titled “Hand the API to an agent”claude mcp add api -- apic mcp --dir ./api --env devThe agent gets list_requests, describe_request, run_request,
run_file, list_environments, clear_session, run_features,
validate_project and curl_request, plus
each .http file as a readable resource. Assertion failures come back as
ok: false rather than tool errors, so the agent can reason about them.
For an agent that only runs shell commands, paste the snippet from
the agents guide into your AGENTS.md or CLAUDE.md.
Drive it from a Taskfile
Section titled “Drive it from a Taskfile”tasks: api: desc: Run an API request, e.g. task api -- get-user --env staging dir: api cmds: - apic run {{.CLI_ARGS}}
api:check: desc: Validate the request files and run the smoke flow dir: api cmds: - apic validate - apic run smoke.http --env {{.ENV}} --jsonKeep dir: on every apic task so they share one session. More patterns in
the Taskfile guide.
Start from a curl command
Section titled “Start from a curl command”Every API’s docs have a curl example; paste it and get a named request:
apic import --curl 'curl -X POST https://api.example.com/todos \ -H "Content-Type: application/json" \ -d "{\"title\": \"x\"}"' --into todos.http --name create-todoThe host becomes {{baseUrl}} when the environment has one, -u becomes
# @auth basic, -F becomes a multipart body, and anything apic cannot
carry (-o, --retry, an unknown flag) is a note on stderr rather than a
failure. On a Mac, pbpaste | apic import --curl - --into todos.http
takes the command straight from the clipboard.
Migrate a Postman collection
Section titled “Migrate a Postman collection”apic import My-API.postman_collection.json -o api --postman-env staging.postman_environment.jsonapic validate -C apiapic list -C apiFolders become files, requests keep their names in kebab-case, the
collection’s variables land in $shared and each environment export
becomes an environment, secrets in the private file. Bearer, basic, AWS
and OAuth2 client-credentials auth become # @auth lines, an API key
becomes its header, and the simple pm.test checks (to.have.status,
pm.expect(jsonData.x).to.eql(...), header checks) become # @assert
lines, with pm.environment.set("token", jsonData.token) becoming
# @capture. What has no equivalent, pre-request scripts and the rest of
the test scripts, is printed as note lines so nothing is lost silently;
the comparison says what to do instead.
Start from an OpenAPI document
Section titled “Start from an OpenAPI document”apic import openapi.yaml -o api --env-name devapic validate -C apiapic list -C apiYou get one .http file per tag, a named request per operation, example
bodies built from the schemas, and an env file with the server URL. Treat it
as a first draft: add # @capture, # @assert and # @auth where they
matter.
Hosted on Cloudflare, which keeps aggregate traffic statistics. No cookies, no tracking scripts, no per-request logs. Privacy