Skip to main content

Setting provider credentials

Every call to a provider needs a credential, usually an API key, and an endpoint. The gateway can take them from several places so that you never have to paste a key into a config file or a request. This page explains where the gateway looks, in which order, and how to store a credential once and reuse it for many models.

What this is for​

A team that runs the gateway usually has a handful of provider accounts and many models. Keeping the key for each provider in one place means a rotated key is changed once, people who add models never see the key, and the key never appears in a request, a log or a config file checked into Git. Stored credentials are encrypted in the database with the key derived from SPANWAY_SALT_KEY, and every read through the API or the Admin UI returns them masked.

Scenario​

A platform team has one Anthropic account for production. They want three deployments on the gateway, a Sonnet model for engineering, a Haiku model for classification and a Haiku model reserved for the support team, all using that one account. They store the Anthropic key once as a credential named anthropic-prod, and every deployment references it by name. When the key is rotated, they update the credential and nothing else changes.

Steps​

  1. Store the credential. In the Admin UI open Models + Endpoints, pick the LLM Credentials tab and click Add Credential. Fill in Credential Name anthropic-prod, Display Name Anthropic production, pick Anthropic as the Provider, paste the key into the API key field and click Add Credential. The same call from the command line is:

    curl -X POST http://localhost:4000/credentials \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{
    "credential_name": "anthropic-prod",
    "display_name": "Anthropic production",
    "credential_values": {"api_key": "sk-ant-api03-your-anthropic-key"},
    "credential_info": {"custom_llm_provider": "anthropic"}
    }'
  2. Add a model that uses it. On the Add Model tab pick Anthropic, choose the model, and under Existing Credentials select anthropic-prod. The provider fields disappear because the credential supplies them. Click Add Model. From the command line, name the credential in spanway_params:

    curl -X POST http://localhost:4000/model/new \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{
    "model_name": "claude-haiku-5-5",
    "spanway_params": {"model": "anthropic/claude-haiku-5-5", "spanway_credential_name": "anthropic-prod"},
    "model_info": {"description": "Fast model for classification"}
    }'
  3. Check what is stored. The LLM Credentials table lists the credential with its provider and DB as the source. From the command line, GET /credentials returns every credential with masked values:

    curl http://localhost:4000/credentials -H "Authorization: Bearer $SPANWAY_API_KEY"
  4. Rotate the key later. Open the credential's ... menu, click Edit, replace the whole masked value with the new key and click Update Credential. Values you send are merged with the stored ones, and to remove a stored value such as an old api_base, list its key in credential_values_to_delete, for example "credential_values_to_delete": ["api_base"]. A key cannot be in both lists. From the command line:

    curl -X PATCH http://localhost:4000/credentials/anthropic-prod \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{
    "display_name": "Anthropic production",
    "credential_values": {"api_key": "sk-ant-api03-your-rotated-key"},
    "credential_info": {"custom_llm_provider": "anthropic"}
    }'

Expected result​

Step 1 answers {"success": true, "message": "Credential created successfully"} and the credential appears in the LLM Credentials table. Step 3 lists it like this.

{
"success": true,
"credentials": [
{
"credential_name": "anthropic-prod",
"display_name": "Anthropic production",
"credential_info": {"custom_llm_provider": "anthropic"},
"credential_values": {"api_key": "sk-ant-****"},
"source": "db"
}
]
}

After step 4 every deployment that references anthropic-prod uses the new key on its next request, with no restart and no change to the models.

Where the gateway looks, in order​

The most specific value wins.

  1. A key written directly into a deployment's spanway_params, such as api_key or api_base.
  2. A stored credential named by the deployment's spanway_params.spanway_credential_name.
  3. The provider's environment variable in the gateway's environment, such as ANTHROPIC_API_KEY or GROQ_API_KEY.
  4. The provider's default endpoint, for providers that have one.

Each provider page lists the exact environment variables its integration reads. Start at providers.

Using environment variables instead​

If you prefer to keep keys in the gateway's environment, reference them from the config file with os.environ/, so the secret never appears in the file. A secret manager can supply the value instead of the environment.

model_list:
- model_name: gpt-6.1-sol
spanway_params:
model: openai/gpt-6.1-sol
api_key: os.environ/OPENAI_API_KEY

Changing the endpoint​

api_base overrides the URL a provider integration calls. Use it for regional endpoints, private networking, Azure resources, self hosted servers and any OpenAI compatible service. Many providers also read an environment variable for it, such as OPENAI_API_BASE, AZURE_API_BASE or OLLAMA_API_BASE, and Azure deployments also need api_version, which can come from AZURE_API_VERSION.

model_list:
- model_name: my-local-model
spanway_params:
model: openai/my-local-model
api_base: http://localhost:8000/v1
api_key: none

When you change the address a stored secret is sent to, send the secret again in the same update. The gateway refuses an update that moves a kept secret to a new api_base, as changing where a stored secret is sent explains.

Checking the environment​

spanway.validate_environment(model="anthropic/claude-sonnet-5-5") returns whether the environment variables the model's provider needs are present and lists any that are missing. The gateway uses the same check when it reports a configuration problem for a model.

Errors​

What you seeWhyFix
400 Credential values are required. Unable to infer credential values from model ID.The request had neither credential_values nor a model_id the gateway could copy values from.Send credential_values, or a model_id of an existing deployment.
409 Credential 'anthropic-prod' already exists.A credential with that name is already stored.Update it with PATCH /credentials/anthropic-prod, or delete it first.
409 Credential 'anthropic-prod' is defined in config. Edit config.yaml to change it.POST /credentials used the name of a credential in config.yaml.Pick another name, or edit the config file and reload the gateway.
405 Credential 'anthropic-prod' is defined in config and cannot be edited from the API or UI.PATCH or DELETE on a credential from config.yaml. The answer carries Allow: GET.Edit the config file and reload the gateway.
400 display_name cannot be blank. ..., display_name cannot be longer than 255 characters. or display_name cannot contain control, formatting, or line separator characters.The label is empty, too long or has hidden characters.Send a short plain label, null to clear it, or leave the field out.
400 credential_values_to_delete overlaps credential_values for key(s): ['api_key']The same key was sent to set and to remove.Put each key in one of the two lists.
403 Only proxy admins can change 'anthropic_federation_rule_id' ...The request touches a workload identity federation field, which only a proxy admin may set.Ask a proxy admin to make the change.
404 Credential not found. Got credential name: anthropic-prodNo credential with that name exists.Check the spelling, or list credentials with GET /credentials.
400 on an update that changes api_baseA stored secret would be sent to a new address without being re-entered.Send the API key again together with the new address.
500 DB not connected. This endpoint needs a databaseCredentials are stored in the database, and the gateway has none.Set DATABASE_URL as described in the production checklist.
A model call fails with an authentication error from the providerThe stored key is wrong or revoked.Rotate the key as in step 4, then use Test Connect on the model.

Audit and logs​

Creating, updating and deleting a stored credential each write an audit row on Spanway_CredentialsTable with who made the change and the values before and after, with every secret masked. A master key rotation that cannot rewrite one credential logs Failed to re-encrypt credential <name>: <reason> at ERROR and leaves it as it was. See audit logs and the log reference.