Skip to main content

API Key Authentication

With API key authentication, clients send an API key with each request to invoke an API proxy. An API key is bound to one API and it authenticates requests to that API only. You generate the key from the API Keys section of the API itself. Consumers can also generate their own keys in the API Portal and AI Hub. See API Keys.

No key manager is involved, and an application is optional. You can associate a key with an application for usage analytics, but this does not change how the key works.

Prerequisites​

Step 1: Attach the API Key Auth Policy​

  1. Sign in to the API Control Plane Console.

  2. In the header, select the project from the Projects list and the API from the APIs list.

  3. In the left navigation menu, click Develop, then click Policies.

  4. In the Available policies pane, find the API Key Auth policy, and drag it to the Global policies section.

  5. Configure where requests carry the API key, such as a header and its name, and click Attach policy.

  6. Click Save changes.

  7. Deploy the API to a gateway, so that the policy takes effect.

For more on attaching policies, including to a single resource, see Attach and Manage Policies.

Step 2: Generate an API Key​

You can generate an API key for an API proxy that is deployed to a gateway. The key is pushed to the gateways that the API proxy is deployed to.

  1. In the left navigation menu, click Overview.

  2. In the API Keys section, click Add.

  3. In the dialog box that opens, enter the following details, and click Create key.

    FieldDescription
    Key nameA name that identifies the key in the list. You cannot change it later.
    Expires inHow long the key is valid, as a number and a unit, such as 90 days.

    Add API key dialog with the Key name field, the Expires in field set to 90 days, and the Cancel and Create key buttons

  4. Copy the key, and store it somewhere safe.

    warning

    The key is shown only once. It is stored hashed, so you cannot view it again after you close the dialog.

  5. Click I have copied the key.

The key appears in the API Keys section, with its name, the last characters of the key, and who created it. The section lists the keys that you created for this API. To revoke a key, click the delete (trash) icon next to it, and in the Revoke API Key dialog box, click Revoke. The gateways reject requests that use a revoked key.

Step 3: Invoke the API with the API Key​

  1. On the Overview page of the API, in the Invoke URL section, select a gateway, and copy the invoke URL.

  2. Send a request to the invoke URL, and include the API key in the header that the API Key Auth policy reads. The default header is API-Key.

    curl -i -H "API-Key: <api-key>" "<invoke-url>/<resource-path>"

    If the key is valid, the gateway forwards the request to the backend and returns the backend's response, such as 200 OK.

If the key is missing, invalid, or expired, the gateway rejects the request with a 401 response and doesn't call the backend:

HTTP/2 401
content-type: application/json

{"error":"Unauthorized","message":"Valid API key required"}

Test with the API Console​

You can also call the API from the API Console without generating a key yourself.

  1. In the left navigation menu, click Test.
  2. Select the gateway.
  3. In the Test key section, click Get Test API Key. The console creates a short-lived key and shows how long it remains valid. It sends the key with every request to that gateway, in the header that the API Key Auth policy reads. If you change the header name in the policy, the console uses the new name.
  4. In the endpoint list, select an operation, and click Send.

To try a key that you generated, add it as a header under Headers in the operation instead.

Was this page helpful?