Quickstart: your first API request
Connect an OpenAI-compatible Python application to 深空 API using a service-issued key and a model ID from the catalog. This example uses Chat Completions; select a model and route that support that API.
1. Prepare your account and model
- Create an account at xkrelay.com.
- Generate a service API key in API keys. A model vendor's own API key is not a substitute.
- Choose an exact model ID and an available group from the model catalog. Check your balance and the group's prices before sending a request.
The SDK base URL is https://xkrelay.com/v1. Do not append /chat/completions to the SDK base URL. Client-specific guides may use a different address format.
2. Configure Python
python -m pip install openai
Set XKRELAY_API_KEY and XKRELAY_MODEL in your local environment or secret manager. Keep keys out of source files, screenshots and version control. The program below validates both values before making a request.
import os
from openai import OpenAI
api_key = os.environ["XKRELAY_API_KEY"]
model = os.environ["XKRELAY_MODEL"]
if not api_key.strip() or not model.strip():
raise ValueError("Set XKRELAY_API_KEY and XKRELAY_MODEL")
client = OpenAI(
base_url="https://xkrelay.com/v1",
api_key=api_key,
)
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(response.choices[0].message.content)
Save the example as hello.py, then run python hello.py. A successful response should contain the assistant's reply. This sends a billable request; begin with a small test and review your usage.
3. Troubleshoot the response
- 401: check that the service-issued key is present, valid and copied without extra whitespace.
- 403: check the response body for account, group or access restrictions. Contact support rather than repeatedly retrying.
- 404 or model not found: verify the exact model ID, selected group, endpoint and protocol.
- 429: check account limits and route capacity. Use bounded retry with backoff where appropriate, not an immediate retry loop.
- Timeout or 5xx: inspect the error body and route status. An interrupted connection does not by itself prove that the request was never processed; check usage before repeating expensive work.
When reporting an error, include the time, model, status and request ID if available. Remove API keys and sensitive prompt content.
Next steps
See service information for capabilities and policy links. Chinese configuration guides are available for Codex, Claude Code and Cherry Studio. Use each tool's documented protocol rather than substituting this Chat Completions example.