How to Use an OpenAI-Compatible API with Python

Clone a runnable repository, configure a protected API key and current model ID, then make one OpenAI-compatible Python request and diagnose common errors.

HeFu · Published 2026-11-26 · Updated 2026-11-28

3 min read

The official OpenAI Python SDK can connect to an OpenAI-compatible endpoint without a vendor-specific wrapper. For HeFu, the essential changes are the base URL, API key and exact model ID.

The runnable example is maintained in the HeFu API Examples repository. This tutorial explains the shortest path from a fresh clone to one verifiable request.

Cost and safety: the quickstart makes one real, billable chat request. Check the current model rate first. Never commit the API key or run the example repeatedly as an automated health check.

1. Clone the Example

git clone https://github.com/hefu-hk/hefu-api-examples.git
cd hefu-api-examples

Install the official SDK in your project or virtual environment:

pip install openai

For an application, pin the tested SDK version in your normal dependency manifest rather than relying on an unpinned global installation.

2. Configure Credentials and a Current Model

Create a HeFu account, create an API key in the console, and open the live model catalog. Copy the complete model ID rather than guessing from the display name.

export HEFU_API_KEY="sk-hefu-..."
export HEFU_MODEL="deepseek-v4-flash"

deepseek-v4-flash is the example default checked against the catalog on 2026-11-28, not a permanent availability promise. Replace it with another currently listed chat model when necessary.

Keep the key in an environment variable, a server-side secret manager or a protected client credential field. Do not place it in browser JavaScript, source code, screenshots or public issue reports.

3. Make One API Call

python quickstart/python_openai_sdk.py

The relevant client configuration is only:

import os

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["HEFU_API_KEY"],
    base_url="https://api.hefu.hk/v1",
)

The script reads HEFU_MODEL, sends a short chat prompt and prints the selected model, answer and reported token usage. The following shows the output shape only; it is not a recorded model response or billing statement:

--- deepseek-v4-flash ---
An API gateway provides a single entry point for routing and managing API requests.
(tokens: 61)

The real answer and token count will vary. Confirm the request in the HeFu console and reconcile actual usage with call records and wallet details.

4. Diagnose the First Request by Error Type

SymptomCheckSafe correction
HEFU_API_KEY is not setThe variable exists in the same shell running PythonExport it again; do not hard-code it in the file
401 UnauthorizedKey is complete, active and sent as a bearer tokenRecreate or rotate the key if needed
404 Not FoundThe SDK receives only the base URLUse https://api.hefu.hk/v1, without appending /chat/completions yourself
Model not foundThe ID may be outdated or differ in separators and capitalizationCopy the exact ID from the live catalog into HEFU_MODEL
429 or 5xxThe error may be transient, but repeated calls can cost moneyUse bounded backoff; do not retry indefinitely

If basic chat works but streaming, tools or structured output fail, test that capability separately. Protocol compatibility at the chat endpoint does not prove that every model supports every advanced feature.

5. Move from a Quickstart to an Integration

Before using real traffic:

  1. Save one sanitized prompt and an objective acceptance check.
  2. Verify the selected model's output quality, context and current billing unit.
  3. Add explicit timeouts, bounded retries and useful error logs without secrets.
  4. Test streaming and tool calls only if your application uses them.
  5. Set a small verification budget and inspect actual console usage.

For model selection, continue with Model Selection and Your First API Call. If the application needs resilience, use the bounded pattern in Model Fallback Without Infinite Retries. The GitHub repository also contains Node.js, curl, OAuth + PKCE and explicitly selected multimodal examples.

FAQ

Do I need a special SDK for HeFu?

No. The example uses the official OpenAI Python SDK and changes the base URL to https://api.hefu.hk/v1. Use the HeFu API key and an exact model ID from the live catalog.

Does the quickstart make a real chargeable request?

Yes. It sends one real chat completion. Check the current model rate before running it and reconcile usage in the console. Do not run it as an unattended test.

How do I replace the default model?

Set HEFU_MODEL to the exact current model ID before running the script. Do not infer an API model ID from a display name.

Related reading

Next step

Prepare one key and one current model ID

Register, create an API key in the console, and check the live catalog and current rate before making the billable test request.

Create a HeFu AccountRead API docs