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.
| Variable | Purpose |
|---|---|
WATSONX_API_BASE or WATSONX_URL | The watsonx.ai endpoint, such as https://us-south.ml.cloud.ibm.com. |
WATSONX_APIKEY or WATSONX_API_KEY | The IBM Cloud API key exchanged for an IAM token. WATSONX_TOKEN can carry a token directly. |
WATSONX_PROJECT_ID | The default project id, when the body carries none. |
Steps
-
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. -
Issue a virtual key on
/ui/api-keyswith + Create New Key, then Create Key. -
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"}' -
Open
/ui/logsand 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
| Status | Message | What it means and how to fix it |
|---|---|---|
401 | Authentication Error, Invalid proxy server token passed | The virtual key is missing, wrong, expired or blocked. |
403 | Key/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. |
404 | Watsonx passthrough config not found | The gateway build has no watsonx pass-through configuration. Contact support@spanway.ai. |
500 | Error: Watsonx URL not set. Set WATSONX_API_BASE in environment variables ... | No endpoint is set. Set WATSONX_API_BASE. |
401 from IBM Cloud | Provided API key could not be found | The IAM exchange failed. Check WATSONX_APIKEY. |
| Any other | watsonx.ai's own error body | watsonx.ai refused the request. The message is returned unchanged. |
Related
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.