# Get started with Gateway

> Put the gateway in front of an API, let one service read invoices through it, and see a read allowed and a delete refused.

In this walkthrough you put the gateway in front of an invoices API for the realm `foobar`. You give the gateway its
identity in the realm, write one policy that lets the `billing-service` client read invoices, and run the gateway
beside the API. At the end, `billing-service` reads an invoice through the gateway, and its attempt to delete one is
refused before it reaches the API.

<Aside type="caution" title="The gateway's image isn't published yet">
  Ask your Cvyūh contact for the gateway's container image before you start. The realm side of this walkthrough, the
  client and the policy, works today without it.
</Aside>

## Before you begin

- **A realm administrator's session.** You're in a group that holds `foobar`'s `RealmOwner` privilege, with your
  session in `$CVYUH_SESSION`: [Call the API](/develop/api/) shows how to get it. Every admin call here goes to
  `https://foobar.cvyuh.com/api/v0/am`. This walkthrough uses the API only.
- **An HTTP API on a Linux host,** listening at `http://127.0.0.1:8801` and answering `GET /invoices/1`. Any API will
  do: the gateway needs nothing from it.
- **On that host:** Docker, `openssl`, `curl` and `jq`, and the gateway's container image, here `$GATEWAY_IMAGE`.
- **An audience for the API.** Callers name it `https://api.example.com`.

## Give the gateway its identity

<Steps>

1. **Make the gateway's key.** On the API's host, make a P-256 key pair, and read the public half's coordinates for the
   realm:

   ```sh
   openssl ecparam -name prime256v1 -genkey -noout | openssl pkcs8 -topk8 -nocrypt -out gateway-key.pem
   b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }
   X=$(openssl ec -in gateway-key.pem -pubout -outform DER 2>/dev/null | tail -c 64 | head -c 32 | b64url)
   Y=$(openssl ec -in gateway-key.pem -pubout -outform DER 2>/dev/null | tail -c 32 | b64url)
   ```

   The private half stays on this host. The realm only ever holds the public half.

2. **Activate the realm's `floor` client.** Every realm is born with this client for the gateway, inactive, holding the
   three scopes the gateway needs. Give it the public key and the two addresses the gateway reads the realm at:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/floor \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
           "jwks": { "keys": [ { "kty": "EC", "crv": "P-256", "kid": "gateway-1", "x": "'$X'", "y": "'$Y'" } ] },
           "allowedResources": ["https://foobar.cvyuh.com/policies/v1", "https://foobar.cvyuh.com/access/v1"],
           "defaultAudience": "https://foobar.cvyuh.com/policies/v1" }'
   ```

   The gateway signs in as `floor` with an assertion signed by its key, so it holds no shared secret. It reads the
   realm's policies and revocations at `/policies/v1`, and asks for the decisions a token can't settle at
   `/access/v1`.

</Steps>

## Let one service read invoices

<Steps>

1. **Register the calling service.** `billing-service` gets tokens for the invoices API with a secret:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=billing-service" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
           "clientName": "Billing service",
           "tokenEndpointAuthMethod": "client_secret_basic",
           "clientSecret": "<a long random secret>",
           "grantTypes": ["client_credentials"],
           "allowedResources": ["https://api.example.com"],
           "defaultAudience": "https://api.example.com" }'
   ```

2. **Write the policy.** It lets `billing-service` read anything under `/invoices/`, on whatever host the gateway
   forwards to:

   ```sh
   curl -i "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=invoices-read" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "active": true,
           "resources": ["*://*:*/invoices/*"],
           "actionValues": { "GET": true },
           "subject": { "type": "JwtClaim", "claimName": "client_id", "claimValue": "billing-service" } }'
   ```

   The answer's `ETag` is the policy's revision.

3. **Link it to the `default` policy set and the `url` resource type.** Each link names the policy's current
   revision, and each answer's `ETag` is the revision the next one names:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/invoices-read/relationships/policySet \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "targetId": "default" }'

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/invoices-read/relationships/resourceType \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "targetId": "url" }'
   ```

</Steps>

## Run the gateway

<Steps>

1. **Start the gateway beside the API.** It listens on port 8080 and forwards what it allows to the API:

   ```sh
   docker run -d --name gateway --network host \
     -v "$PWD/gateway-key.pem:/etc/gateway/key.pem:ro" \
     -e ISSUER=https://foobar.cvyuh.com/oauth2 \
     -e JWT_AUDIENCE=https://api.example.com \
     -e CLIENT_ID=floor \
     -e CLIENT_KEY=/etc/gateway/key.pem \
     -e POLICY_SET=default \
     -e BACKEND=http://127.0.0.1:8801 \
     "$GATEWAY_IMAGE"
   ```

   Every setting is in [Gateway configuration](/gateway/configuration/).

2. **Give it a few seconds.** Until its first policies and revocation list arrive, the gateway refuses everything.

</Steps>

<Aside type="caution" title="Close the side door">
  The gateway protects only what goes through it. Once callers use the gateway, make sure the API can't be reached
  except through it.
</Aside>

## See it decide

<Steps>

1. **Call without a token.** The gateway refuses, and says where to find out how to get one:

   ```sh
   curl -i http://127.0.0.1:8080/invoices/1
   ```

   ```http
   HTTP/1.1 401 Unauthorized
   WWW-Authenticate: Bearer resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
   WWW-Authenticate: DPoP algs="ES256 PS256 RS256", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
   ```

2. **Get a token as `billing-service`.** It's an access token for `https://api.example.com`, good for five minutes:

   ```sh
   TOKEN=$(curl -s https://foobar.cvyuh.com/oauth2/token \
     -u "billing-service:<the secret>" \
     -d grant_type=client_credentials \
     -d resource=https://api.example.com | jq -r .access_token)
   ```

3. **Read an invoice.** The policy allows it, so the gateway forwards it, and you get the API's own answer:

   ```sh
   curl -i http://127.0.0.1:8080/invoices/1 -H "Authorization: Bearer $TOKEN"
   ```

   The API receives the request with `x-agent-id: billing-service`, the verified caller, which no client can set for
   itself.

4. **Try to delete it.** No policy allows `DELETE`, so the gateway refuses it, and the API never sees it:

   ```sh
   curl -i -X DELETE http://127.0.0.1:8080/invoices/1 -H "Authorization: Bearer $TOKEN"
   ```

   ```http
   HTTP/1.1 403 Forbidden
   WWW-Authenticate: Bearer error="insufficient_scope", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
   ```

   The body gives the reason, `no_matching_policy`.

</Steps>

## Check it worked

- Set `billing-service`'s `status` to `inactive` and get a new token: the realm refuses it. A token it already holds
  keeps working at the gateway until it expires: see
  [A client you set inactive](/gateway/revocation/what-can-be-revoked/#a-client-you-set-inactive).
- Change the policy's `actionValues` to `{ "GET": true, "DELETE": true }`: within a minute, the gateway's next policy
  poll, the delete reaches the API.

## If it doesn't work

| What you see | Why | What to do |
|---|---|---|
| Every call with a token is `503 revocations_stale` | The gateway can't sign in to the realm, so it has no revocation list yet | Check that `floor` is `active` and holds the gateway's public key, and that `CLIENT_KEY` is the matching private half |
| The read is `403 no_policies_loaded` | The policy isn't linked to `default`, or isn't linked to `url` | Make both links |
| The read is `403 policies_excluded` | The policy is linked to `default` but not yet to `url`, so the set is incomplete and allows nothing | Link it to `url` |
| The read is `401 invalid_token` | The token's audience isn't the gateway's `JWT_AUDIENCE`, or it's from another realm | Get the token from `https://foobar.cvyuh.com/oauth2/token` for the same audience the gateway names |

Every symptom, with its fix, is in [Troubleshooting](/gateway/protect-apis/run-the-gateway/#troubleshooting).

<Aside type="tip" title="Undo it">
  Stop the gateway (`docker rm -f gateway`), point callers back at the API, delete the `invoices-read` policy, and set
  `floor`'s `status` to `inactive`.
</Aside>

## Next steps

<CardGrid>
  <LinkCard title="Open an API to agents" description="Agents call through the gateway, and a stopped agent is refused within seconds." href="/gateway/guides/open-an-api-to-agents/" />
  <LinkCard title="Protect an MCP server" description="One tool allowed and another refused, on the same server." href="/gateway/guides/protect-an-mcp-server/" />
  <LinkCard title="Requests and refusals" description="Every refusal a caller can meet, and what your API receives." href="/gateway/protect-apis/requests-and-refusals/" />
</CardGrid>
