Skip to main content

Require an API Key

Refuse every request that does not carry a key you issued. Each client has its own key, so that a key can be withdrawn without disturbing the others.

How it works​

The gateway compares a key sent by the caller with a list you store. A request without a key from the list is answered 401 before it reaches the service. Each caller should have its own key, so that one can be withdrawn without affecting the others.

A key never expires on its own

Plan its rotation: add the new key to the list, move the caller, then remove the old one.

Every parameter and its default: reference.

Use cases​

For a small number of known applications, when a key in a header is enough. For APIs called by many applications, or when a token must expire, use tokens.

Prerequisites​

  • An API instance that is deployed and answers.
  • A list of keys: a stored configuration of the kind API Keys. It can be created from the rule.

Procedure​

  1. In API Manager, go to API Instances and open the API. To cover everything published on a gateway instead, open the gateway.
  2. Open its Security policies and add the rule API keys.
  3. In API Keys, choose the list of keys, or New secret to create one. Add one entry per client and copy each key: it is shown once.
  4. Say where callers send the key: fill Extract from Header with x-api-key, or use a query parameter or a cookie.
  5. Optionally fill Forward Client ID Header and turn Sanitize Key on.
  6. Deploy.

Parameters​

FieldWhat it isDefault
API KeysThe stored list of keys, one per client.—
Extract from HeaderHeader in which callers send the key, such as x-api-key.—
Extract from Query ParamQuery parameter in which callers send the key, such as api_key.—
Extract from CookieCookie in which callers send the key.—
Forward Client ID HeaderHeader in which the name of the client is passed to your service, such as x-client-id.—
Sanitize KeyRemove the key from the request before it reaches your service.Off

Verification​

Without a key
curl -i https://orders.example.com/orders
Answer
HTTP/1.1 401 Unauthorized
With a key
curl -i -H "x-api-key: <key>" https://orders.example.com/orders
Answer
HTTP/1.1 200 OK

Behavior and limits​

  • A key does not expire. Remove a client from the list to withdraw its key.
  • A key in a query parameter ends up in logs and browser histories: prefer a header.
  • Keys are kept as fingerprints and cannot be read back: copy them when they are made.

See also​