# Client SDKs

The OpenAI library and the Anthropic library work against OpenDunes with two fields swapped: the address and the key.

1. [Install a library](#1-install-a-library).
2. [Swap the address and the key](#2-swap-the-address-and-the-key).
3. [Send with the OpenAI library](#3-send-with-the-openai-library).
4. [Send with the Anthropic library](#4-send-with-the-anthropic-library).
5. [Read the charge and the balance](#5-read-the-charge-and-the-balance).

## 1. Install a library

| Tab | OpenAI library | Anthropic library |
|---|---|---|
| Python | `pip install openai` | `pip install anthropic` |
| JavaScript | `npm install openai` | `npm install @anthropic-ai/sdk` |
| Go | `go get github.com/openai/openai-go/v3` | `go get github.com/anthropics/anthropic-sdk-go` |
| cURL | none | none |

## 2. Swap the address and the key

| Library | Address | Key |
|---|---|---|
| OpenAI library | `https://opendunes.com/v1` | `OPENDUNES_API_KEY` |
| Anthropic library | `https://opendunes.com` | `OPENDUNES_API_KEY` |

Everything else in your code stays as it is. The OpenAI library sends a conversation to [`POST /v1/chat/completions`](/docs/reference/chat-completions), the Anthropic library to [`POST /v1/messages`](/docs/reference/messages). [Create a key](/docs/api-keys) first if you have none.

<div class="watch-out">
<p>The Anthropic library adds <code>/v1</code> itself. Give it <code>https://opendunes.com</code>, without <code>/v1</code>.</p>
</div>

## 3. Send with the OpenAI library

```python
import os
from openai import OpenAI

API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"

client = OpenAI(base_url=BASE_URL, api_key=API_KEY)

raw = client.chat.completions.with_raw_response.create(
    model=MODEL,
    messages=[
        {"role": "user", "content": "Two lines to welcome a shop in Oran."},
    ],
)
reply = raw.parse()

print(reply.choices[0].message.content)
print(reply.usage.cost)
print(raw.headers["X-Balance-Available"])
```

```javascript
import OpenAI from "openai";

const API_KEY = process.env.OPENDUNES_API_KEY;
const BASE_URL = "https://opendunes.com/v1";
const MODEL = "anthropic/claude-sonnet-4.5";

const client = new OpenAI({ baseURL: BASE_URL, apiKey: API_KEY });

const { data: reply, response } = await client.chat.completions
  .create({
    model: MODEL,
    messages: [
      { role: "user", content: "Two lines to welcome a shop in Oran." },
    ],
  })
  .withResponse();

console.log(reply.choices[0].message.content);
console.log(reply.usage.cost);
console.log(response.headers.get("X-Balance-Available"));
```

```go
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/openai/openai-go/v3"
	"github.com/openai/openai-go/v3/option"
)

const (
	baseURL = "https://opendunes.com/v1"
	model   = "anthropic/claude-sonnet-4.5"
)

func main() {
	apiKey := os.Getenv("OPENDUNES_API_KEY")

	client := openai.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey(apiKey),
	)

	params := openai.ChatCompletionNewParams{
		Model: model,
		Messages: []openai.ChatCompletionMessageParamUnion{
			openai.UserMessage("Two lines to welcome a shop in Oran."),
		},
	}
	var res *http.Response
	reply, err := client.Chat.Completions.New(
		context.TODO(), params, option.WithResponseInto(&res),
	)
	if err != nil {
		panic(err)
	}

	fmt.Println(reply.Choices[0].Message.Content)
	fmt.Println(reply.Usage.JSON.ExtraFields["cost"].Raw())
	fmt.Println(res.Header.Get("X-Balance-Available"))
}
```

```bash cURL
ADDRESS="https://opendunes.com/v1/chat/completions"
MODEL="anthropic/claude-sonnet-4.5"

curl -i "$ADDRESS" \
  -H "Authorization: Bearer $OPENDUNES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'"$MODEL"'",
    "messages": [
      {
        "role": "user",
        "content": "Two lines to welcome a shop in Oran."
      }
    ]
  }'
```

With `-i`, [`X-Balance-Available`](/docs/reference/headers) is among the headers, and [`usage.cost`](/docs/balance-and-charges) is in the body.

## 4. Send with the Anthropic library

```python
import os
from anthropic import Anthropic

API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com"
MODEL = "anthropic/claude-sonnet-4.5"

client = Anthropic(base_url=BASE_URL, api_key=API_KEY)

raw = client.messages.with_raw_response.create(
    model=MODEL,
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Two lines to welcome a shop in Oran."},
    ],
)
message = raw.parse()

print(message.content[0].text)
print(message.usage.cost)
print(raw.headers["X-Balance-Available"])
```

```javascript
import Anthropic from "@anthropic-ai/sdk";

const API_KEY = process.env.OPENDUNES_API_KEY;
const BASE_URL = "https://opendunes.com";
const MODEL = "anthropic/claude-sonnet-4.5";

const client = new Anthropic({ baseURL: BASE_URL, apiKey: API_KEY });

const { data: message, response } = await client.messages
  .create({
    model: MODEL,
    max_tokens: 1024,
    messages: [
      { role: "user", content: "Two lines to welcome a shop in Oran." },
    ],
  })
  .withResponse();

console.log(message.content[0].text);
console.log(message.usage.cost);
console.log(response.headers.get("X-Balance-Available"));
```

```go
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/anthropics/anthropic-sdk-go"
	"github.com/anthropics/anthropic-sdk-go/option"
)

const (
	baseURL = "https://opendunes.com"
	model   = "anthropic/claude-sonnet-4.5"
	prompt  = "Two lines to welcome a shop in Oran."
)

func main() {
	apiKey := os.Getenv("OPENDUNES_API_KEY")

	client := anthropic.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey(apiKey),
	)

	params := anthropic.MessageNewParams{
		Model:     model,
		MaxTokens: 1024,
		Messages: []anthropic.MessageParam{
			anthropic.NewUserMessage(anthropic.NewTextBlock(prompt)),
		},
	}
	var res *http.Response
	message, err := client.Messages.New(
		context.TODO(), params, option.WithResponseInto(&res),
	)
	if err != nil {
		panic(err)
	}

	fmt.Println(message.Content[0].Text)
	fmt.Println(message.Usage.JSON.ExtraFields["cost"].Raw())
	fmt.Println(res.Header.Get("X-Balance-Available"))
}
```

```bash cURL
ADDRESS="https://opendunes.com/v1/messages"
MODEL="anthropic/claude-sonnet-4.5"

curl -i "$ADDRESS" \
  -H "x-api-key: $OPENDUNES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'"$MODEL"'",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Two lines to welcome a shop in Oran."
      }
    ]
  }'
```

With `-i`, `X-Balance-Available` is among the headers, and `usage.cost` is in the body.

## 5. Read the charge and the balance

These are the two fields only OpenDunes returns; the libraries pass them through, and the samples above read them.

| Field | Where | Holds |
|---|---|---|
| `usage.cost` | the answer | the charge, in DA |
| `X-Balance-Available` | the header | your balance after it, in µDA |

<div class="good-to-know">
<p>A streamed answer carries no <code>X-Balance-Available</code>. Its charge is <code>usage.cost</code>, in the closing event: see <a href="/docs/streaming">Streaming</a>.</p>
</div>

Working in a framework? [Frameworks](/docs/frameworks) gives the two fields each needs.
