Code Examples
Production-ready snippets. Pick a language tab, copy, run.
Basic Chat
chatbot.py
import requests
resp = requests.post("https://oiv1.bcworks.in.net/v1/chat/completions", headers={
"Authorization": "Bearer your_api_key_here",
"Content-Type": "application/json"
}, json={
"messages": [{"role": "user", "content": "What is machine learning?"}],
"max_tokens": 150, "temperature": 0.7
})
print(resp.json()["choices"][0]["message"]["content"])
client.mjs
const resp = await fetch("https://oiv1.bcworks.in.net/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer your_api_key_here",
"Content-Type": "application/json"
},
body: JSON.stringify({
messages: [{ role: "user", content: "What is machine learning?" }],
max_tokens: 150
})
});
const data = await resp.json();
console.log(data.choices[0].message.content);
bash
curl -X POST https://oiv1.bcworks.in.net/v1/chat/completions \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello"}]}' | jq '.'
powershell
$headers = @{ "Authorization" = "Bearer your_api_key_here" }
$body = @{
messages = @(@{ role = "user"; content = "What is AI?" })
max_tokens = 150
} | ConvertTo-Json -Depth 5
$resp = Invoke-RestMethod -Uri "https://oiv1.bcworks.in.net/v1/chat/completions" `
-Method POST -Headers $headers -Body $body -ContentType "application/json"
Write-Host $resp.choices[0].message.content
main.go
package main
import (
"bytes"; "encoding/json"; "fmt"; "net/http"
)
func main() {
body, _ := json.Marshal(map[string]any{
"messages": []map[string]string{{"role": "user", "content": "What is AI?"}},
})
req, _ := http.NewRequest("POST",
"https://oiv1.bcworks.in.net/v1/chat/completions",
bytes.NewBuffer(body))
req.Header.Set("Authorization", "Bearer your_api_key_here")
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
var result map[string]any
json.NewDecoder(resp.Body).Decode(&result)
choices := result["choices"].([]any)
msg := choices[0].(map[string]any)["message"].(map[string]any)
fmt.Println(msg["content"])
}
chat.php
<?php
$ch = curl_init("https://oiv1.bcworks.in.net/v1/chat/completions");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer your_api_key_here",
"Content-Type: application/json"
],
CURLOPT_POSTFIELDS => json_encode([
"messages" => [["role" => "user", "content" => "What is AI?"]],
"max_tokens" => 150
])
]);
$resp = json_decode(curl_exec($ch), true);
curl_close($ch);
echo $resp["choices"][0]["message"]["content"];
Image Analysis
analyze.py
import requests, base64
with open("photo.jpg", "rb") as f:
img = base64.b64encode(f.read()).decode()
resp = requests.post(
"https://oiv1.bcworks.in.net/v1/chat/completions",
headers={"Authorization": "Bearer your_api_key_here"},
json={"messages": [{
"role": "user", "content": "Describe this image",
"image": f"data:image/jpeg;base64,{img}"
}], "max_tokens": 200}
)
print(resp.json()["choices"][0]["message"]["content"])
analyze.mjs
import { readFileSync } from "fs";
const img = readFileSync("photo.jpg").toString("base64");
const resp = await fetch("https://oiv1.bcworks.in.net/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer your_api_key_here",
"Content-Type": "application/json"
},
body: JSON.stringify({
messages: [{
role: "user", content: "Describe this image",
image: `data:image/jpeg;base64,${img}`
}], max_tokens: 200
})
});
const data = await resp.json();
console.log(data.choices[0].message.content);
bash
IMG=$(base64 -w 0 photo.jpg)
curl -X POST https://oiv1.bcworks.in.net/v1/chat/completions \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d "{\"messages\":[{\"role\":\"user\",\"content\":\"Describe this image\",\"image\":\"data:image/jpeg;base64,$IMG\"}],\"max_tokens\":200}" \
| jq -r '.choices[0].message.content'
Advanced Patterns
async_client.py
import aiohttp, asyncio
API = "https://oiv1.bcworks.in.net/v1/chat/completions"
HDR = {"Authorization": "Bearer your_api_key_here", "Content-Type": "application/json"}
async def ask(session, q):
async with session.post(API, headers=HDR, json={
"messages": [{"role": "user", "content": q}]
}) as r:
data = await r.json()
return data["choices"][0]["message"]["content"]
async def main():
async with aiohttp.ClientSession() as s:
results = await asyncio.gather(ask(s, "What is AI?"), ask(s, "What is ML?"))
for r in results: print(r)
asyncio.run(main())
server.mjs
import express from "express";
const app = express();
app.use(express.json());
app.post("/api/chat", async (req, res) => {
const resp = await fetch("https://oiv1.bcworks.in.net/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.OI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ messages: req.body.messages, max_tokens: 150 })
});
res.json(await resp.json());
});
app.listen(3000, () => console.log("Listening on :3000"));
chat.rb
require 'net/http'; require 'json'; require 'uri'
uri = URI("https://oiv1.bcworks.in.net/v1/chat/completions")
req = Net::HTTP::Post.new(uri, {
"Authorization" => "Bearer your_api_key_here",
"Content-Type" => "application/json"
})
req.body = { messages: [{ role: "user", content: "What is AI?" }] }.to_json
resp = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |h| h.request(req) }
puts JSON.parse(resp.body)["choices"][0]["message"]["content"]
Program.cs
using System.Text; using System.Text.Json;
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer your_api_key_here");
var body = JsonSerializer.Serialize(new {
messages = new[] { new { role = "user", content = "What is AI?" } },
max_tokens = 150
});
var resp = await client.PostAsync(
"https://oiv1.bcworks.in.net/v1/chat/completions",
new StringContent(body, Encoding.UTF8, "application/json"));
var json = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
Console.WriteLine(json.RootElement
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetString());