---
title: Use an API key from an environment variable
description: Move an existing API call to the gateway or bind the key when your code must use the provider's host directly.
slug: /guides/use-an-api-key-from-an-environment-variable
order: 60
visibility: public
redirects_from: []
---

# Use an API key from an environment variable

Send API calls through the gateway, even if your code already reads a provider key from a variable such as `WEATHER_API_KEY`. The gateway adds the stored key to the call. Bind the provider key to the variable only if you require the client to keep its fixed host, or the library fixes that host and gives you no way to set the gateway address.

## Move the call to the gateway

1. Find the stored key's name with `list_secrets`, or [store the key](/cloud/getting-started/store-a-secret/#2-supply-the-value-without-exposing-it). In the manifest, look for the stored name in a `secret` value under `settings`. For a key scoped to this application, `list_secrets` also lists manifest bindings in `settings` and the running copy’s bindings in `applied_settings`.

   If you find a binding, supply the key again from your original source under a new stored name such as `WEATHER_UPSTREAM_KEY`. The platform keeps the stored value private. If an upstream declaration returns `upstream_key_is_bound`, its `detail` identifies the binding, including one in a deploy, promote or redeploy in progress. Use a new stored name in that case too.
2. Choose how your code will call the gateway.

   - **Change the request.** Build the URL from `TURNZERO_CLOUD_GATEWAY_URL` (fall back to `TURNZERO_CLOUD_API`), then append `/egress/v0/<upstream>` and the provider's path. Send `TURNZERO_CLOUD_APP_TOKEN` as `Authorization: Bearer` and remove the read of `WEATHER_API_KEY`. Keep the old binding while code that reads it is still running. [See the request before and after](/cloud/guides/call-an-external-api-with-an-api-key/#move-code-that-reads-the-key-from-an-environment-variable).

   - **Keep the variable names.** If the code or SDK reads its base URL and key from variables and sends the key as `Authorization: Bearer`, put those variable names in `sdk_settings.base_url` and `sdk_settings.key` on the upstream. The next deploy, promote, or restart gives the new copy the gateway URL and any egress key under those names. Replacing the provider key under its existing stored name takes effect on gateway calls. Remove any manifest `settings` entries for those variables as part of the change. [See the SDK declaration](/cloud/guides/call-an-external-api-with-an-api-key/#an-unchanged-sdk).

3. [Declare the upstream](/cloud/guides/call-an-external-api-with-an-api-key/#2-declare-the-upstream) with the stored name as `credential_name` and any `sdk_settings` you chose. If the manifest owns the declaration, add it to `upstreams` and submit the manifest with any `settings` changes. Otherwise, submit any `settings` changes first, then call `declare_outbound_api`. Deploy only after the declaration is in place.
4. Deploy and call the route that uses the API. If you deploy to development first, check production after promotion. Keep the old host in `egress` and keep the old stored key while a running copy or a deployment in progress still needs them. Keep `sdk_settings.key` while a running copy uses the upstream's egress key. Removing that setting ends the upstream's outstanding egress keys. [Follow the rollout and cleanup steps](/cloud/guides/call-an-external-api-with-an-api-key/#move-code-that-reads-the-key-from-an-environment-variable).

## Bind the key for a direct call

Use these steps when your client meets either fixed-host condition above.

1. Store the key for this application and the environment that will use it. If it is stored only at account scope, supply it again under a new name at application scope.
2. In the manifest's `settings`, set the existing variable name to an object such as `{"secret": "WEATHER_PROVIDER_KEY"}`, where `WEATHER_PROVIDER_KEY` is the stored name. Add the provider host to `egress`. [See how to bind a stored value](/cloud/guides/author-the-manifest/#bind-a-stored-value-to-a-setting).
3. Submit the manifest, deploy, and call the route that uses the key. Keep the key out of chat, source code, and logs.

## Refusals

| Refusal | Status | Cause | Remedy |
| --- | --- | --- | --- |
| `upstream_key_is_bound` | 409 | A setting already uses the stored name in the manifest, a running copy, or a deploy, promote, or redeploy in progress. | Store the key under a new name and use that name as `credential_name`. |
