Calling your agent from Python, Node.js or curl: the examples in Use through the API

All in One AIAgents Through the API·· 5 views· 3 min read

Each agent has a Use through the API section, on the Agents tab of All in One AI. There you find the agent id, with a Copy button, and three ready-made examples: curl, Python and Node.js. The examples already include the API address and the name of the key variable: INTERWEB_API_KEY.

The three steps in the section

1 "Create a key with the ai:write permission". The Create key in the API Center button takes you there. Keep the key in the INTERWEB_API_KEY variable.
2 "Put the agent in the model field". Copy the agent/... with the Copy button.
3 "Send the request". It works with the OpenAI SDK and with streaming. With a sandbox_ key, the answer is a test one and uses no credits.

Keep the key in the variable

On Windows, open Command Prompt or PowerShell and type (with your key in quotes):

setx INTERWEB_API_KEY "prod_..."

setx saves the variable for the windows you open afterwards. Close the terminal and the tool, then open them again.

On Linux and macOS, type in the terminal:

export INTERWEB_API_KEY="prod_..."

To have the variable in every new terminal, add the same line at the end of ~/.bashrc or ~/.zshrc.

curl

In a Linux or macOS terminal:

curl "https://api.interweb.ao/v1/chat/completions" \
-H "Authorization: Bearer $INTERWEB_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "agent/7c1e9a42-b35d8f06-a2c4e719-5d0b3f8e", "messages": [{"role": "user", "content": "Hi! What time do you open tomorrow?"}]}'

On Windows, use the Python or Node.js example, which work the same way on every system.

Python

Install the OpenAI SDK with pip install openai, save the code as agent.py and run python agent.py:

import os
from openai import OpenAI

client = OpenAI(base_url="https://api.interweb.ao/v1", api_key=os.environ["INTERWEB_API_KEY"])

completion = client.chat.completions.create(
model="agent/7c1e9a42-b35d8f06-a2c4e719-5d0b3f8e",
messages=[{"role": "user", "content": "Hi! What time do you open tomorrow?"}],
)
print(completion.choices[0].message.content)

Node.js

Install the SDK with npm install openai, save the code as agent.mjs and run node agent.mjs. The .mjs ending is needed because the example uses import and await outside a function:

import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.interweb.ao/v1", apiKey: process.env.INTERWEB_API_KEY });

const completion = await client.chat.completions.create({
model: "agent/7c1e9a42-b35d8f06-a2c4e719-5d0b3f8e",
messages: [{ role: "user", content: "Hi! What time do you open tomorrow?" }],
});
console.log(completion.choices[0].message.content);

In all three, the answer is in choices[0].message.content and the token count in usage.

The examples in the section do not include max_tokens. Add it if you want shorter answers than the agent's maximum: the request can lower that value, never raise it. To show the text as it is written, see an agent's answer with streaming.

Want your agent answering inside your program? See All in One AI.

See All in One AI

SEE ALSO

Using your AI agent through the API: agent/ in the model field

Using the OpenAI SDK with All in One AI: only the address and the key change

An agent's answer with streaming: why the first piece can take longer

Creating an API key: name, mode, permissions, validity, IP list and spending limit

All in One AI

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.59/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.