CanalAPI
Developer Guide

API quickstart

Send your first request with PowerShell, curl, JavaScript, or Python, then check the Responses endpoint.

Prerequisites

Create a key and check your credit using getting started. These requests may incur charges. Replace the placeholders with your actual key and the full ID of a model that supports Chat Completions.

Set variables and run the examples in the same terminal. These assignments last for the current session only. Keep keys out of repositories and chat messages.

Windows PowerShell

$env:CANALAPI_BASE_URL = "https://api.canalapi.com/v1"
$env:CANALAPI_API_KEY = "YOUR_CANALAPI_KEY"
$env:CANALAPI_MODEL = "YOUR_MODEL_ID"

macOS / Linux / WSL

export CANALAPI_BASE_URL="https://api.canalapi.com/v1"
export CANALAPI_API_KEY="YOUR_CANALAPI_KEY"
export CANALAPI_MODEL="YOUR_MODEL_ID"

Windows PowerShell

Use Invoke-RestMethod to avoid Windows PowerShell's curl alias and JSON quoting differences. After receiving text, check the request and model in your usage logs.

$body = @{
  model = $env:CANALAPI_MODEL
  messages = @(@{ role = "user"; content = "Reply only OK" })
} | ConvertTo-Json -Depth 10
$response = Invoke-RestMethod `
  -Uri "$env:CANALAPI_BASE_URL/chat/completions" `
  -Method Post `
  -Headers @{ Authorization = "Bearer $env:CANALAPI_API_KEY" } `
  -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) `
  -TimeoutSec 60
$response.choices[0].message.content

curl

This example is for Bash on macOS, Linux, WSL, or Git Bash, not PowerShell. It requires curl 7.76+ for --fail-with-body; check with curl --version.

curl --fail-with-body --max-time 60 \
  "$CANALAPI_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $CANALAPI_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "model": "$CANALAPI_MODEL",
  "messages": [{"role": "user", "content": "Reply only OK"}]
}
JSON

JavaScript (Node 22+)

Install the SDK in your example directory, save the code as quickstart.mjs, and run it. Automatic retries are disabled to expose the original error during setup. Configure bounded retries for production later.

npm install openai
quickstart.mjs
import OpenAI from 'openai';

for (const name of ['CANALAPI_API_KEY', 'CANALAPI_BASE_URL', 'CANALAPI_MODEL']) {
  if (!process.env[name]) throw new Error(`Missing environment variable: ${name}`);
}

const client = new OpenAI({
  apiKey: process.env.CANALAPI_API_KEY,
  baseURL: process.env.CANALAPI_BASE_URL,
  timeout: 60_000,
  maxRetries: 0,
});

const completion = await client.chat.completions.create({
  model: process.env.CANALAPI_MODEL,
  messages: [{ role: 'user', content: 'Reply only OK' }],
});
console.log(completion.choices[0].message.content);
node quickstart.mjs

Python

Install the SDK in your Python environment, save the code as quickstart.py, and run it. If your system uses python3, replace python in both installation and execution commands.

python -m pip install openai
quickstart.py
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["CANALAPI_API_KEY"],
    base_url=os.environ["CANALAPI_BASE_URL"],
    timeout=60.0,
    max_retries=0,
)
response = client.chat.completions.create(
    model=os.environ["CANALAPI_MODEL"],
    messages=[{"role": "user", "content": "Reply only OK"}],
)
print(response.choices[0].message.content)
python quickstart.py

Check Responses for Codex

Set CANALAPI_MODEL to a model confirmed to support Responses, then choose the request matching your terminal. A successful Chat Completions request does not replace this check. Raw REST content is in the output array; the SDK's output_text convenience property is not a guaranteed field in the raw JSON.

$body = @{
  model = $env:CANALAPI_MODEL
  input = "Reply only OK"
} | ConvertTo-Json
$response = Invoke-RestMethod `
  -Uri "$env:CANALAPI_BASE_URL/responses" `
  -Method Post `
  -Headers @{ Authorization = "Bearer $env:CANALAPI_API_KEY" } `
  -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) `
  -TimeoutSec 60
$response.output | ConvertTo-Json -Depth 20
curl --fail-with-body --max-time 60 \
  "$CANALAPI_BASE_URL/responses" \
  -H "Authorization: Bearer $CANALAPI_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{"model": "$CANALAPI_MODEL", "input": "Reply only OK"}
JSON

This checks a basic Responses request only. Codex also needs streaming, tool support, and compatible model metadata. Continue with the Codex guide to verify tool behavior.

What next

  • After a normal response and matching usage log, try streaming.
  • On failure, use connection troubleshooting. Record the status, error type, and X-Request-Id header without printing credentials.
  • Before production, configure timeouts, concurrency, and bounded retries. Never embed keys in browser code.

On this page