Skip to main content

watsonx.ai pass-through

The /watsonx route forwards watsonx.ai paths, such as /ml/v1/text/generation and /ml/v1/text/tokenization, to watsonx.ai. IBM Cloud expects a short-lived IAM token, so the gateway exchanges its own API key for one and refreshes it as needed, and adds the version query parameter when the caller leaves it out. Callers only need a virtual key, and every call is recorded with its cost.

A real-world scenario​

A bank's document team uses watsonx.ai text generation from an internal tool that cannot run the IBM token exchange itself. The tool calls /watsonx/ml/v1/text/generation on the gateway with a virtual key. The gateway handles the IAM token, the IBM Cloud key never leaves it, and the team's spend is visible on the Logs page.

Before you start​

The gateway needs the watsonx.ai endpoint and credential, and the project the requests run in.

VariablePurpose
WATSONX_API_BASE or WATSONX_URLThe watsonx.ai endpoint, such as https://us-south.ml.cloud.ibm.com.
WATSONX_APIKEY or WATSONX_API_KEYThe IBM Cloud API key exchanged for an IAM token. WATSONX_TOKEN can carry a token directly.
WATSONX_PROJECT_IDThe default project id, when the body carries none.

Steps​

  1. Give the gateway the credential. Open /ui/models-and-endpoints, on the Add Model tab pick Watsonx under Provider, pick a model, enter the endpoint, key and project id, open Advanced Settings, turn on Use in pass through routes and click Add Model. Or set the variables above and restart the gateway.

  2. Issue a virtual key on /ui/api-keys with + Create New Key, then Create Key.

  3. Call watsonx.ai through the gateway.

    curl -X POST http://localhost:4000/watsonx/ml/v1/text/generation \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"model_id": "mistralai/mistral-large", "input": "Summarise this ticket in one sentence.", "project_id": "a1b2c3d4-0000-1111-2222-333344445555"}'
  4. Open /ui/logs and click the newest row to see the request, the key and its cost.

Expected result​

watsonx.ai's own response, with its status code and headers. Streaming is streamed through as it arrives.

{
"model_id": "mistralai/mistral-large",
"results": [{"generated_text": "The customer wants one of two duplicate charges refunded.", "generated_token_count": 12, "input_token_count": 18, "stop_reason": "eos_token"}]
}

The spend log row carries the key, team and user and the cost from the token counts.

Errors​

StatusMessageWhat it means and how to fix it
401Authentication Error, Invalid proxy server token passedThe virtual key is missing, wrong, expired or blocked.
403Key/team denied access to passthrough route /watsonx/...The key or team lists the route in denied_passthrough_routes. The message goes on with Matched <pattern> in denied_passthrough_routes, naming the pattern to remove from the key or team.
404Watsonx passthrough config not foundThe gateway build has no watsonx pass-through configuration. Contact support@spanway.ai.
500Error: Watsonx URL not set. Set WATSONX_API_BASE in environment variables ...No endpoint is set. Set WATSONX_API_BASE.
401 from IBM CloudProvided API key could not be foundThe IAM exchange failed. Check WATSONX_APIKEY.
Any otherwatsonx.ai's own error bodywatsonx.ai refused the request. The message is returned unchanged.

Pass-through endpoints lists every built-in route. The unified chat completions endpoint works with every provider and runs guardrails. The watsonx.ai provider page covers the unified integration.