# SDK examples

Noduma's API with curl, the OpenAI Python SDK and the OpenAI JavaScript SDK.

Source: https://docs.noduma.dev/sdk-examples/

Noduma serves a subset of OpenAI's chat-completions interface, so the OpenAI SDKs work by changing the base URL to `https://app.noduma.dev/v1` and using a Noduma API key. curl and the OpenAI Python SDK were tested against the API; the JavaScript examples send the same requests through the official `openai` package and have not been run against it yet.

Every example reads the key from `NODUMA_API_KEY`.

## Setup

**curl**

```sh
export NODUMA_API_KEY=ndm_k_...
```

**Python**

```python
# pip install openai
import os
from openai import OpenAI

client = OpenAI(base_url="https://app.noduma.dev/v1", api_key=os.environ["NODUMA_API_KEY"])
```

**JavaScript**

```js
// npm install openai

const client = new OpenAI({ baseURL: 'https://app.noduma.dev/v1', apiKey: process.env.NODUMA_API_KEY });
```

## List the models

**curl**

```sh
curl https://app.noduma.dev/v1/models -H "Authorization: Bearer $NODUMA_API_KEY"
```

**Python**

```python
for model in client.models.list():
    print(model.id, model.model_extra.get("noduma"))
```

**JavaScript**

```js
const models = await client.models.list();
for (const model of models.data) console.log(model.id, model.noduma);
```

## Stream an answer

**curl**

```sh
curl -N https://app.noduma.dev/v1/chat/completions \
  -H "Authorization: Bearer $NODUMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen2.5-7b-instruct-q4_k_m", "stream": true,
       "messages": [{"role": "user", "content": "Write a haiku about shared GPUs."}]}'
```

**Python**

```python
stream = client.chat.completions.create(
    model="qwen2.5-7b-instruct-q4_k_m",
    messages=[{"role": "user", "content": "Write a haiku about shared GPUs."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\ncost:", chunk.usage.model_extra.get("noduma_cost"), "credits")
```

**JavaScript**

```js
const stream = await client.chat.completions.create({
  model: 'qwen2.5-7b-instruct-q4_k_m',
  messages: [{ role: 'user', content: 'Write a haiku about shared GPUs.' }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
  if (chunk.usage) console.log('\ncost:', chunk.usage.noduma_cost, 'credits');
}
```

The last chunk before `[DONE]` carries `usage`, with the answer's cost. See [Streaming](/api/streaming/).

## Use a tool

**curl**

```sh
curl https://app.noduma.dev/v1/chat/completions \
  -H "Authorization: Bearer $NODUMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3-coder-30b-a3b-instruct-q4_k_m",
    "messages": [{"role": "user", "content": "What is the weather in Madrid?"}],
    "tools": [{"type": "function", "function": {
      "name": "get_weather",
      "description": "Current weather of a city",
      "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
    }}]
  }'
```

**Python**

```python
tools = [{"type": "function", "function": {
    "name": "get_weather",
    "description": "Current weather of a city",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}}]
answer = client.chat.completions.create(
    model="qwen3-coder-30b-a3b-instruct-q4_k_m",
    messages=[{"role": "user", "content": "What is the weather in Madrid?"}],
    tools=tools,
)
for call in answer.choices[0].message.tool_calls or []:
    print(call.function.name, call.function.arguments)
```

**JavaScript**

```js
const tools = [{ type: 'function', function: {
  name: 'get_weather',
  description: 'Current weather of a city',
  parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] },
} }];
const answer = await client.chat.completions.create({
  model: 'qwen3-coder-30b-a3b-instruct-q4_k_m',
  messages: [{ role: 'user', content: 'What is the weather in Madrid?' }],
  tools,
});
for (const call of answer.choices[0].message.tool_calls ?? []) console.log(call.function.name, call.function.arguments);
```

Your program runs the tool and sends its result back as a `tool` message. See [Tools](/api/tools/).

## Ask only your own PCs

With the `Noduma-Routing: own` header only the PCs paired to your account answer, free. See [Routing](/concepts/routing/).

**curl**

```sh
curl https://app.noduma.dev/v1/chat/completions \
  -H "Authorization: Bearer $NODUMA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Noduma-Routing: own" \
  -d '{"model": "qwen2.5-7b-instruct-q4_k_m", "messages": [{"role": "user", "content": "Hello"}]}'
```

**Python**

```python
answer = client.chat.completions.create(
    model="qwen2.5-7b-instruct-q4_k_m",
    messages=[{"role": "user", "content": "Hello"}],
    extra_headers={"Noduma-Routing": "own"},
)
```

**JavaScript**

```js
const answer = await client.chat.completions.create(
  { model: 'qwen2.5-7b-instruct-q4_k_m', messages: [{ role: 'user', content: 'Hello' }] },
  { headers: { 'Noduma-Routing': 'own' } },
);
```

## Read your balance

**curl**

```sh
curl https://app.noduma.dev/v1/account -H "Authorization: Bearer $NODUMA_API_KEY"
```

**Python**

```python
import json, os, urllib.request

request = urllib.request.Request("https://app.noduma.dev/v1/account",
                                 headers={"Authorization": "Bearer " + os.environ["NODUMA_API_KEY"]})
print(json.load(urllib.request.urlopen(request))["available"])
```

**JavaScript**

```js
const res = await fetch('https://app.noduma.dev/v1/account', {
  headers: { Authorization: `Bearer ${process.env.NODUMA_API_KEY}` },
});
console.log((await res.json()).available);
```
