Lesson 6: Authentication
Goal. Attach credentials to requests with # @auth, per request and
per project, and know what apic shows and what it hides.
You will need
Section titled “You will need”- apic installed (lesson 1)
apic demorunning in another terminal, or start it now:
apic demo --out apic-demoThe demo API accepts any bearer token, HTTP basic auth for alice, an
API key, and a client-credentials token endpoint, so every kind of auth in
this lesson runs offline. The two real providers at the end are prose.
1. Bearer, by hand and by directive
Section titled “1. Bearer, by hand and by directive”You have been doing auth since lesson 1. whoami writes the header
itself:
GET {{baseUrl}}/meAuthorization: Bearer {{token}}The directive form does the same thing:
# @auth bearer {{token}}GET {{baseUrl}}/meTwo differences. A directive can be a project default (step 5), so you do
not repeat the header in every request. And whatever apic adds for you is
never printed: only headers you wrote yourself appear in -v, and even
those are masked when they carry a credential:
apic run login -C apic-demo > /dev/nullapic run whoami -C apic-demo -v | head -3GET http://localhost:8089/meAuthorization: ***(The login first is so that whoami’s # @ref login has nothing to do;
on a fresh session it would run login and print that report before its
own.)
2. Basic
Section titled “2. Basic”Basic auth is the one you cannot write as a plain header, because the
value is base64 of user:password. Let apic do it:
# @auth basic {{user}} {{password}}GET {{baseUrl}}/basic-auth/{{user}}/{{password}}apic run basic-auth -C apic-demoGET http://localhost:8089/basic-auth/alice/s3cret200 OK · 1 ms · 23 B
{ "authenticated": true}
✓ status == 200✓ body.$.authenticated == trueapic curl maps every auth type onto curl where curl has an equivalent,
which for basic is --user:
apic curl basic-auth -C apic-democurl -sS \ --user 'alice:s3cret' \ 'http://localhost:8089/basic-auth/alice/s3cret'3. An API key
Section titled “3. An API key”An API key is a header with a secret in it. Write the header, keep the value in the private file, and apic masks it because of where it came from:
GET {{baseUrl}}/keyedX-Api-Key: {{apiKey}}apic run api-key -C apic-demo -v | head -3GET http://localhost:8089/keyedX-Api-Key: ***Authorization, X-Api-Key, Cookie and a few other header names are
always masked, and so is any header whose value came from a secret source:
the private env file, .env, the session, a capture, --var or
APIC_VAR_.
4. OAuth2 client credentials
Section titled “4. OAuth2 client credentials”Machine-to-machine OAuth2 is one line. The demo has a token endpoint:
# @auth oauth2 tokenUrl={{baseUrl}}/oauth/token clientId={{clientId}} clientSecret={{clientSecret}}GET {{baseUrl}}/medescribe shows the spec and whether its variables resolve, before
anything is sent:
apic describe oauth2-token -C apic-demoGET {{baseUrl}}/meClient-credentials token, fetched and cached automaticallyfile: auth.http:34
auth oauth2 tokenUrl={{baseUrl}}/oauth/token clientId={{clientId}} clientSecret={{clientSecret}} (request)
variables ✓ baseUrl http://localhost:8089 http-client.env.json [local] ✓ clientId demo-client auth.http:2 @clientId ✓ clientSecret *** http-client.private.env.json [local]Run it, then look at the session:
apic run oauth2-token -C apic-demo > /dev/nullapic session -C apic-demolocal ↳ $oauth2:ba9f21f030e5a6f3 = token, expires in 1h0m0s ↳ token = mock-tokenapic fetched a token from the token endpoint, sent the request with it,
and cached the token in the session keyed by token URL, client id, grant
and scope. Every later request with the same spec reuses it until a minute
before it expires, then refreshes it. apic session clear forgets it;
--no-session fetches a fresh one every time.
In --json, the request records which auth type applied and nothing else
about it:
apic run oauth2-token -C apic-demo --json | grep -o '"auth":"[a-z0-9]*"'"auth":"oauth2"A real provider. The repository’s examples/spotify project does this
against the Spotify Web API with grant=client_credentials clientAuth=basic, the client id and secret in the private file, and
apic session showing the cached token with its lifetime. Entra ID, Okta,
Auth0, Keycloak and Cognito all speak the same grant, so the line is the
same with a different tokenUrl.
A human at a keyboard. grant=device_code prints a URL and a code,
waits while you approve in a browser, and caches the token like any other.
It is for people, not agents: an unattended run would wait until the code
expired. grant=authorization_code (with PKCE) is the other one for
people: it opens a browser once and caches the token, and it never
starts under --json, MCP or without a terminal; see
auth.md.
5. A project default, and opting out
Section titled “5. A project default, and opting out”Most projects authenticate every request the same way. Say it once in
apic.yaml instead of on each request. Make a small project beside the
demo to see it (the demo API accepts any bearer token, so a fixed one in
the private file will do here):
base=$(grep -o 'http://[^"]*' apic-demo/http-client.env.json | head -1)mkdir -p apic-authcat > apic-auth/http-client.env.json <<EOF{ "local": { "baseUrl": "$base" } }EOFcat > apic-auth/http-client.private.env.json <<'EOF'{ "local": { "token": "mock-token" } }EOFcat > apic-auth/apic.yaml <<'EOF'env: localauth: default: bearer {{token}}EOFcat > apic-auth/api.http <<'EOF'### Uses the project default# @name me# @assert status == 200GET {{baseUrl}}/me
### Public, so it opts out# @name health# @auth none# @assert status == 200GET {{baseUrl}}/healthEOFapic run me health -C apic-auth | tail -4✓ me 200 1 ms✓ health 200 0 ms
2 passed · 2 requests · 1 msme has no auth line and no Authorization header, and was sent with the
bearer token from the default. health said # @auth none and was sent
with nothing. A request with its own # @auth basic … would use that
instead. describe names the source:
apic describe me -C apic-auth | sed -n 5,6pauth bearer {{token}} (apic.yaml)Because the default may hold {{variables}}, one project can point at a
different identity provider per environment: tokenUrl and clientId in
the public env file, clientSecret in the private one, and a default of
oauth2 tokenUrl={{tokenUrl}} clientId={{clientId}} clientSecret={{clientSecret}}.
6. A token from any command
Section titled “6. A token from any command”When the credential comes from a tool (gcloud, az, op, vault),
# @auth exec runs it and uses its output:
cat >> apic-auth/api.http <<'EOF'
### A token from a command# @name me-exec# @auth exec echo mock-token# @assert status == 200GET {{baseUrl}}/meEOFapic validate -C apic-authapic run me-exec -C apic-auth || echo "exit $?"api.http:14:9: warning: @auth exec will be refused until apic.yaml sets auth.allowExec: true (exec-disabled)! 1 file, 3 requests, 1 warningGET {{baseUrl}}/meerror: api.http:16: auth: @auth exec is disabled: set `auth: allowExec: true` in apic.yaml to let request files run "echo mock-token" hint: Check the # @auth spec (or auth.default), its credentials, and the token endpoint. E105 authentication failed · see https://apic.sh/errors/#e105exit 2Refused, on purpose: a request file that runs commands deserves a deliberate yes, because agents edit request files. The project opts in:
cat > apic-auth/apic.yaml <<'EOF'env: localauth: default: bearer {{token}} allowExec: trueEOFapic run me-exec -C apic-auth | head -2GET http://localhost:8089/me200 OK · 1 ms · 30 BThe command runs directly, not through a shell, and its trimmed output
becomes the Authorization: Bearer … header; header= and prefix=
change that, and ttl=30m caches the output in the session.
7. AWS Signature V4
Section titled “7. AWS Signature V4”Signing a request for API Gateway, S3 or Lambda is one directive, and apic
finds the keys the way the AWS CLI does: the environment, then
~/.aws/credentials, then the CLI itself for SSO sessions and assumed
roles. There is no AWS SDK inside apic; the signature is implemented and
checked against the official test vectors.
# @auth aws service=execute-api region=eu-west-2GET {{baseUrl}}/ordersWithout an AWS account, apic curl still shows what the signed request
would be, with fake keys in the environment:
cat >> apic-auth/api.http <<'EOF'
### Signed for API Gateway# @name signed# @auth aws service=execute-api region=eu-west-2GET {{baseUrl}}/healthEOFAWS_ACCESS_KEY_ID=AKIAEXAMPLE AWS_SECRET_ACCESS_KEY=example apic curl signed -C apic-authcurl -sS \ --aws-sigv4 'aws:amz:eu-west-2:execute-api' \ --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \ ${AWS_SESSION_TOKEN:+-H} \ ${AWS_SESSION_TOKEN:+x-amz-security-token:$AWS_SESSION_TOKEN} \ 'http://localhost:8089/health'Against a real API Gateway with a real key pair, apic run signed sends
the Authorization: AWS4-HMAC-SHA256 … header, X-Amz-Date and
X-Amz-Content-Sha256, and shows none of them in its output. The
cookbook has the full recipe
and the FAQ the usual reasons
for a 403.
Checkpoint
Section titled “Checkpoint”The OAuth2 request goes through the whole flow, token endpoint included, and reports the auth type it used:
apic run oauth2-token -C apic-demo --json | grep -o '"auth":"oauth2"'"auth":"oauth2"Exercise
Section titled “Exercise”Move the demo project onto a default. Set auth.default: bearer {{token}}
in apic-demo/apic.yaml, delete every Authorization: Bearer {{token}}
header from todos.http, and make apic run todos.http --keep-going and
apic test pass as before. One request will need to opt out; find it.
Solution
apic-demo/apic.yaml:
env: localauth: default: bearer {{token}}Delete the Authorization: line from each request in todos.http.
Then run apic test -C apic-demo and every scenario fails: login
now needs {{token}} too, because the default applies to every request
without its own # @auth, and login is the request that produces the
token. It opts out:
### Log in and keep the token# @name login# @auth noneNow the flow shows 1 failed, 11 passed (the deliberate not-found)
and apic test -C apic-demo passes every scenario. Requests with their
own # @auth, like basic-auth and oauth2-token, were never
affected: a request’s own directive wins over the default.
Going further
Section titled “Going further”- Authentication, every type and option
- OAuth2 client credentials and AWS API Gateway with SigV4 in the cookbook
- What appears in output
Episode script
Length. 14 minutes. The Spotify and AWS segments are prerecorded and short.
Cold open (0:00). apic run whoami -v with Authorization: ***.
“apic will add a credential to a request five different ways, and
never print one. Here are all five.”
Talking points.
- Bearer by hand and by directive; why the directive exists.
- Basic on the demo, and
apic curlturning it into--user. - The API key header, masked because of where the value came from.
- OAuth2:
describe, run,apic sessionwith the cached token and its lifetime,"auth":"oauth2"in JSON. Cut to Spotify for thirty seconds: the same line, a real token. - A project default in
apic.yaml,# @auth none,describenaming the source. exec: refused, then allowed, and why it is off by default.- AWS: the directive,
apic curlwith fake keys, then a prerecorded call to a real API Gateway. - Checkpoint, then the exercise: the default on the demo and the one request that must opt out.
Shot list. One terminal, 100x30, font size 16;
docs/learn/tapes/06.tape reproduces the demo-only parts. Picture in
picture for the Spotify developer dashboard and the AWS console.
Chapters. 0:00 Five ways, never printed · 0:50 Bearer · 2:00 Basic · 3:00 API keys · 4:00 OAuth2 · 7:00 A project default ·
8:40 exec · 10:20 AWS SigV4 · 12:30 Checkpoint and exercise.
Description. From the episode template.
Hosted on Cloudflare, which keeps aggregate traffic statistics. No cookies, no tracking scripts, no per-request logs. Privacy