This tutorial demonstrates a common Chat Completions-style interface. It is a generic example, not a verified RunAPI integration guide, and may not work with every model platform. Confirm the endpoint, authentication, and request shape in the selected service’s documentation.
1. Prepare three settings
Find the complete request URL, available model identifier, and account key in the provider’s official documentation. Use only an account you are authorized to use. Configure the key locally or on a server; never upload it to a public repository.
# PowerShell: affects only the current terminal process
$env:LLM_ENDPOINT = "https://provider.example/the-complete-documentation-endpoint"
$env:LLM_MODEL = "the-model-identifier-from-the-documentation"
$env:LLM_API_KEY = "your-key"
Replace the domain and model name; these placeholders cannot run as written. Be mindful of terminal history and screen sharing when entering a key. In production, use a controlled secret-management system.
2. Send a request with the standard library
import json
import os
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError
payload = {
"model": os.environ["LLM_MODEL"],
"messages": [{"role": "user", "content": "Explain what a token is in two sentences."}]
}
req = Request(
os.environ["LLM_ENDPOINT"],
data=json.dumps(payload).encode("utf-8"),
headers={
"Authorization": "Bearer " + os.environ["LLM_API_KEY"],
"Content-Type": "application/json"
},
method="POST"
)
try:
with urlopen(req, timeout=60) as response:
result = json.load(response)
print(result["choices"][0]["message"]["content"])
except HTTPError as exc:
print("HTTP error:", exc.code)
except URLError:
print("Network connection failed; check the URL and network.")
except (KeyError, IndexError, TypeError):
print("Missing configuration or a different response shape; check the provider documentation.")If a platform uses another authentication header, message format, or response field, adapt the code. There are no automatic retries here, so a configuration error cannot silently create repeated charges.
3. Diagnose errors
- 401: The key may be invalid, expired, or formatted incorrectly.
- 403: Check permissions, region restrictions, and the provider’s explanation.
- 404: Check the endpoint path; some services also use it for an unknown model.
- 429: A rate or quota limit may apply; read the provider’s error details first.
- 5xx or timeout: Record the time and request ID, then use limited exponential-backoff retries; never loop forever.
4. Make testing reproducible
Record the exact model, input, output, latency, and bill. Do not put credentials or sensitive responses in public logs. A successful test proves only that this request worked; it does not prove long-term stability.