> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openfinance.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Inference API Examples

> Copy these examples to make your first inference request, stream a reply, list the models and check your credits, with curl or the OpenAI SDK you already use.

Every example below uses the base URL `https://api.openfinance.tech/agent/v1` and reads your key from an environment variable. For what the API is and how billing works, see the [overview](https://docs.openfinance.tech/inference/overview.md). Set the key once in your shell:

```bash theme={"system"}
export OPENFINANCE_API_KEY="open_..."
```

## A first request

Here is the same request in curl, Python and TypeScript:

```bash curl theme={"system"}
curl https://api.openfinance.tech/agent/v1/chat/completions \
  -H "Authorization: Bearer $OPENFINANCE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-fable-5.1",
    "messages": [{ "role": "user", "content": "Say hello in five words." }]
  }'
```

```python Python theme={"system"}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["OPENFINANCE_API_KEY"],
    base_url="https://api.openfinance.tech/agent/v1",
)

res = client.chat.completions.create(
    model="anthropic/claude-fable-5.1",
    messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(res.choices[0].message.content)
```

```ts TypeScript theme={"system"}
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.OPENFINANCE_API_KEY,
  baseURL: "https://api.openfinance.tech/agent/v1",
});

const res = await client.chat.completions.create({
  model: "anthropic/claude-fable-5.1",
  messages: [{ role: "user", content: "Say hello in five words." }],
});
console.log(res.choices[0].message.content);
```

The only differences from a stock OpenAI setup are the key and the base URL. Request and response shapes stay the same.

## Streaming

Set `stream` to `true` to receive the reply as it is written.

```python Python theme={"system"}
stream = client.chat.completions.create(
    model="anthropic/claude-fable-5.1",
    messages=[{"role": "user", "content": "Explain funding rates in two sentences."}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
```

```ts TypeScript theme={"system"}
const stream = await client.chat.completions.create({
  model: "anthropic/claude-fable-5.1",
  messages: [{ role: "user", content: "Explain funding rates in two sentences." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
```

## List the models

```bash curl theme={"system"}
curl https://api.openfinance.tech/agent/v1/models \
  -H "Authorization: Bearer $OPENFINANCE_API_KEY"
```

```python Python theme={"system"}
for model in client.models.list():
    print(model.id)
```

Use an id from this list as the `model` value. Ids use a `provider/model` form.

## Check your credits

```bash theme={"system"}
curl https://api.openfinance.tech/agent/chat/credits \
  -H "x-api-key: $OPENFINANCE_API_KEY"
```

Look at `availableCredits` in the response. Top up in the OpenFinance app when it runs low; see [Models and AI credits](https://docs.openfinance.tech/product/models-and-credits.md).

## Switching from another provider

If your code already uses an OpenAI-compatible client, change two lines:

```diff theme={"system"}
  const client = new OpenAI({
-   apiKey: process.env.OTHER_PROVIDER_API_KEY,
-   baseURL: "https://api.other-provider.com/v1",
+   apiKey: process.env.OPENFINANCE_API_KEY,
+   baseURL: "https://api.openfinance.tech/agent/v1",
  });
```

Then replace the model name with an id from `GET /models`.

## Tips

* **Either header works.** `Authorization: Bearer open_...` suits OpenAI-style clients. `x-api-key: open_...` matches what you already send to the MCP server.
* **Run it server-side.** The key can also trade and move funds, so it must never reach a browser or a mobile app.
* **Handle rejected requests.** If a request is rejected for an invalid key, check that the key starts with `open_`. If it is rejected for insufficient credits, top up and retry.

## Next

> [card] [Inference overview](https://docs.openfinance.tech/inference/overview.md)
>
> Endpoints, auth and credits.

> [card] [MCP overview](https://docs.openfinance.tech/mcp/overview.md)
>
> Let an AI client trade through OpenFinance.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.