Standard Library
api
Make outbound HTTP requests. Backed by libcurl, with JSON parsed into native objects.
Import:
import "api";curl
Send a request and get the parsed response. The first argument is a URL; the second is an options object.
object res = curl("https://dogapi.dog/api/v2/facts?limit=1", {});
res->data[0]->attributes->body | print;Options
All options are optional:
methodGET, POST, PUT, DELETE, etc. Default GET.headersAn object of header name to value.bodyAn object that will be sent as JSON.POST with a JSON body
curl("https://api.example.com/items", {
method: "POST",
headers: {
Authorization: "Bearer " + token
},
body: {
name: "widget",
price: 9.99
}
}) | print;Calling an LLM
Most chat APIs follow the OpenAI shape, so a single curl call works against many providers:
import "dotenv";
string url = envvar("AI_URL");
string model = envvar("AI_MODEL");
string key = envvar("AI_API_KEY");
curl(url, {
method: "POST",
headers: { Authorization: "Bearer " + key },
body: {
model: model,
messages: [
{ role: "system", content: "You are friendly." },
{ role: "user", content: "Hi!" }
]
}
}) | print;