EN ▾
Get API key

Curl AI API Examples: Chat, Stream, JSON and Tools

Curl is the fastest way to test any curl ai api: one command shows you the status code, the headers and the raw JSON, with no SDK in the way. These recipes cover chat, streaming, JSON mode, tool calls and balance checks against an OpenAI-compatible endpoint.

Updated

A terminal sending glowing packets into a server, with a stream of tokens coming back, representing curl API calls
A glowing agent node looping data with three tool modules, illustrating a LangChain tool-calling cycle

Key points

  • LangChain's ChatOpenAI class works with any OpenAI-compatible endpoint through base_url.
  • An agent is a bounded loop: model call, tool execution, repeat until no tool calls remain.
  • JSON mode gives machine-readable output for planner and router steps.
  • A six-step agent run costs well under half a cent at our token prices.

Setup: put the key in a variable

Keep the key out of your command history by exporting it once per shell session. The examples below use $KEY. The base URL is https://api.unrestrictedai.cc/v1 and the model ID is uncensored. You can get a key from the API key page, and new users can claim $0.50 of trial credit there with one click and no card.

export KEY="paste-your-key-here"

Install jq if you want readable JSON in the terminal. It is optional, but it makes the output far easier to scan. The examples pipe through it where useful.

Recipe 1: a first chat request

This is the smallest useful call. The -s flag silences the progress meter, and jq extracts the reply text:

curl -s https://api.unrestrictedai.cc/v1/chat/completions   -H "Authorization: Bearer $KEY"   -H "Content-Type: application/json"   -d '{
    "model": "uncensored",
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Give me one tip for writing shell scripts."}
    ],
    "temperature": 0.7
  }' | jq -r '.choices[0].message.content'

Drop the jq part to see the whole object. You will find choices, the message, a finish_reason and a usage block with prompt and completion token counts. Those counts are what you are billed on. The max_tokens field is optional here, and without it the completion can use the rest of the 100,000-token window, so add it when you want a hard cap on output cost.

The API is stateless, so it remembers nothing between calls. For a multi-turn chat, send the whole conversation each time: the earlier user messages and the assistant replies, in order, followed by the new question. This is also why long chats cost more per turn, since the history counts as input tokens every time. To make tests repeatable, pass a seed and a low temperature. Sampling parameters such as top_p, stop and the penalties are passed through to the model, so you can try them from the command line before you wire them into code.

Recipe 2: streaming

With "stream": true the server sends server-sent events, one small JSON chunk per line prefixed with data:, and a final data: [DONE]. Curl buffers output by default, so add -N to disable buffering and see tokens as they arrive:

curl -N https://api.unrestrictedai.cc/v1/chat/completions   -H "Authorization: Bearer $KEY"   -H "Content-Type: application/json"   -d '{
    "model": "uncensored",
    "stream": true,
    "stream_options": {"include_usage": true},
    "messages": [{"role": "user", "content": "Count from one to five, one number per line."}]
  }'

Usage is reported at the end of the stream. A streamed request keeps its concurrency slot until the stream ends, so if you run several in parallel from a script, remember the limit: 8 at once on a full key, 2 on a trial key.

Recipe 3: JSON mode

When a program reads the answer, ask for JSON. Set response_format to the json_object type and also say in the prompt that you want JSON. Models follow the format better when the instruction appears in words too.

curl -s https://api.unrestrictedai.cc/v1/chat/completions   -H "Authorization: Bearer $KEY"   -H "Content-Type: application/json"   -d '{
    "model": "uncensored",
    "response_format": {"type": "json_object"},
    "messages": [
      {"role": "user", "content": "Return a JSON object with keys city and country for the capital of France."}
    ]
  }' | jq -r '.choices[0].message.content | fromjson'

The fromjson filter parses the string inside the message, which fails loudly if the model returned something that is not valid JSON. That makes it a handy check while you tune your prompt.

Recipe 4: tool calls

Tool calling is a two-step exchange. First you describe a function. If the model decides to use it, the reply contains a tool_calls entry instead of text:

curl -s https://api.unrestrictedai.cc/v1/chat/completions   -H "Authorization: Bearer $KEY"   -H "Content-Type: application/json"   -d '{
    "model": "uncensored",
    "messages": [{"role": "user", "content": "What is the weather in Oslo?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }
    }],
    "tool_choice": "auto"
  }' | jq '.choices[0].message.tool_calls'

Your code runs the function and sends the result back in a second request. The message list now holds the original question, the assistant message with its tool_calls, and a message with role set to tool, a matching tool_call_id and the result as content. The model then writes the final answer. The same flow works with streaming, where the call arrives in pieces. For a longer agent example, see the article on agentic AI with LangChain.

Reading headers, balance and errors

Add -i to print response headers. Four are useful: X-Request-Id to quote when you contact support, X-Balance-USD for your remaining credit, and X-RateLimit-Limit-Requests and X-RateLimit-Limit-Concurrency for your limits.

curl -i https://api.unrestrictedai.cc/v1/chat/completions   -H "Authorization: Bearer $KEY"   -H "Content-Type: application/json"   -d '{"model":"uncensored","messages":[{"role":"user","content":"ping"}],"max_tokens":5}'

# balance without a chat request (same base URL, same key)
curl -s https://api.unrestrictedai.cc/v1/balance -H "Authorization: Bearer $KEY"

The OpenAI-style route /v1/dashboard/billing/credit_grants also works for balance, and GET /v1/models lists what the key can use. Common failures:

  • 401: missing or wrong key, often a stray space or quote.
  • 429 with rate_limited: more than the allowed requests per minute (300 on a full key, 60 on a trial key).
  • 429 with concurrency: too many at once.
  • 429 with Retry-After: 5: temporary overload; wait five seconds and retry.
  • Request body too large: the limit is 8 MB.

What your test calls cost

Curl tests are cheap. Pricing is $0.15 per 1M input tokens and $0.50 per 1M output tokens, and failed requests are not charged. A test with 200 input tokens and 300 output tokens costs 200 x $0.15 / 1,000,000 + 300 x $0.50 / 1,000,000 = $0.00003 + $0.00015 = $0.00018. Even a hundred such tests cost under two cents, so the trial credit goes a long way. Top-ups start at $20; see the pricing page and the documentation for every parameter, and the integration guide when you are ready to move from curl to an SDK.

Questions and answers

How do I send an API key with curl?

Add the header -H "Authorization: Bearer $KEY" to the request. Store the key in an environment variable instead of typing it into the command.

Why does curl show no output while streaming?

Curl buffers output. Add the -N flag so each event prints as it arrives.

Can I use the same curl commands with the OpenAI API?

The request shape is the same. Change the base URL, key and model name, since this endpoint serves the model uncensored.

How do I check my balance from the terminal?

Call GET /v1/balance with your key, or read the X-Balance-USD header on any response.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.