Every template below is a complete working file. Set NORTHGATE_API_KEY and run — copy-paste time to first successful API call is under 10 minutes, no docs required.
AI Chatbot
PythonNode.js
One-line setup for a multi-turn chatbot. Defaults to the cheapest first-hop provider and lets the SDK retry transient failures.
# chatbot.py — multi-turn chatbot via the NorthGate SDK
# 1. pip install northgate-sdk
# 2. export NORTHGATE_API_KEY=ngk_your_key_here
# 3. python chatbot.py
import os
from northgate import NorthgateClient
client = NorthgateClient(
api_key=os.environ["NORTHGATE_API_KEY"],
base_url="https://northgate-ai.polsia.app/v1",
route="balanced",
)
history = [
{"role": "system", "content": "You are a helpful assistant. Keep answers under 80 words."},
]
print("Chatbot ready. Type 'quit' to exit.\n")
while True:
user = input("you> ").strip()
if not user or user.lower() == "quit":
break
history.append({"role": "user", "content": user})
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=history,
temperature=0.7,
max_tokens=200,
)
reply = response["choices"][0]["message"]["content"]
history.append({"role": "assistant", "content": reply})
print(f"bot> {reply}\n")
// chatbot.js — multi-turn chatbot via the NorthGate SDK
// 1. npm install northgate-sdk
// 2. export NORTHGATE_API_KEY=ngk_your_key_here
// 3. node chatbot.js
const readline = require("readline");
const { NorthgateClient } = require("northgate-sdk");
const client = new NorthgateClient({
apiKey: process.env.NORTHGATE_API_KEY,
baseUrl: "https://northgate-ai.polsia.app/v1",
route: "balanced",
});
const history = [
{ role: "system", content: "You are a helpful assistant. Keep answers under 80 words." },
];
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
console.log("Chatbot ready. Type 'quit' to exit.\n");
async function ask(prompt) {
return new Promise((resolve) => rl.question(prompt, resolve));
}
(async () => {
while (true) {
const user = (await ask("you> ")).trim();
if (!user || user.toLowerCase() === "quit") break;
history.push({ role: "user", content: user });
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: history,
temperature: 0.7,
max_tokens: 200,
});
const reply = response.choices[0].message.content;
history.push({ role: "assistant", content: reply });
console.log("bot> " + reply + "\n");
}
rl.close();
})();
# Run it
pip install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
python text-summarizer.py
npm install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
node text-summarizer.js
Code Reviewer
PythonNode.js
Reviews a diff with a system prompt + user message. Routes to Together AI via Qwen2.5-Coder-32B-Instruct.
# code_reviewer.py — review a git diff with a senior-engineer system prompt
# 1. pip install northgate-sdk
# 2. export NORTHGATE_API_KEY=ngk_your_key_here
# 3. git diff | python code_reviewer.py OR python code_reviewer.py diff.patch
import os, sys
from northgate import NorthgateClient
diff = sys.stdin.read() if not sys.stdin.isatty() else open(sys.argv[1]).read()
if "diff --git" not in diff:
print("Provide a unified diff via stdin or a .patch file.", file=sys.stderr)
sys.exit(1)
client = NorthgateClient(
api_key=os.environ["NORTHGATE_API_KEY"],
base_url="https://northgate-ai.polsia.app/v1",
route="best_quality",
)
response = client.chat.completions.create(
model="Qwen/Qwen2.5-Coder-32B-Instruct",
messages=[
{
"role": "system",
"content": (
"You are a senior engineer doing thorough code review. For each change:\n"
"- Point out bugs, edge cases, and security issues.\n"
"- Suggest concrete fixes with code snippets.\n"
"- Be terse. Numbered list. No preamble."
),
},
{"role": "user", "content": f"Review this diff:\n\n{diff}"},
],
max_tokens=800,
temperature=0.2,
)
print(response["choices"][0]["message"]["content"])
// code_reviewer.js — review a git diff with a senior-engineer system prompt
// 1. npm install northgate-sdk
// 2. export NORTHGATE_API_KEY=ngk_your_key_here
// 3. git diff | node code_reviewer.js OR node code_reviewer.js diff.patch
const fs = require("fs");
const { NorthgateClient } = require("northgate-sdk");
(async () => {
const diff = process.argv[2] ? fs.readFileSync(process.argv[2], "utf8") : fs.readFileSync(0, "utf8");
if (!diff.includes("diff --git")) { console.error("Provide a unified diff via stdin or a .patch file."); process.exit(1); }
const client = new NorthgateClient({
apiKey: process.env.NORTHGATE_API_KEY,
baseUrl: "https://northgate-ai.polsia.app/v1",
route: "best_quality",
});
const response = await client.chat.completions.create({
model: "Qwen/Qwen2.5-Coder-32B-Instruct",
messages: [
{
role: "system",
content:
"You are a senior engineer doing thorough code review. For each change:\n" +
"- Point out bugs, edge cases, and security issues.\n" +
"- Suggest concrete fixes with code snippets.\n" +
"- Be terse. Numbered list. No preamble."
},
{ role: "user", content: "Review this diff:\n\n" + diff },
],
max_tokens: 800,
temperature: 0.2,
});
console.log(response.choices[0].message.content);
})();
# Run it
pip install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
python code-reviewer.py
npm install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
node code-reviewer.js
Semantic Search (Embeddings)
PythonNode.js
Embeds query + corpus against POST /v1/embeddings and ranks by cosine similarity. Uses raw fetch — the SDK does not yet wrap embeddings.
# semantic_search.py — embed a query, rank a small corpus by cosine similarity
# 1. pip install requests (the SDK does not yet wrap embeddings)
# 2. export NORTHGATE_API_KEY=ngk_your_key_here
# 3. python semantic_search.py "your search query"
import os, sys, json, urllib.request, math
api_key = os.environ["NORTHGATE_API_KEY"]
base_url = "https://northgate-ai.polsia.app/v1"
query = sys.argv[1] if len(sys.argv) > 1 else "how do I rotate my API key?"
corpus = [
"POST /auth/key/regenerate-session rotates your API key; the old key is invalidated the moment a new one is issued.",
"NorthGate enforces per-key rate limits: 100/day (developer), 10k/month (team), 100k/month (enterprise).",
"Streaming responses arrive as Server-Sent Events; each line is a chunk you can append to the assistant message.",
"Embeddings use POST /v1/embeddings and return a float vector per input; cosine similarity ranks candidates.",
"Audit logs never store raw prompts — only token counts, latency, route, and a SHA-256 hash of the client IP.",
]
def embed(texts):
data = json.dumps({"model": "text-embedding-3-small", "input": texts}).encode()
req = urllib.request.Request(
f"{base_url}/embeddings",
data=data,
headers={"Content-Type": "application/json", "X-API-Key": api_key},
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as r:
return [d["embedding"] for d in json.loads(r.read())["data"]]
def cos(a, b):
return sum(x * y for x, y in zip(a, b)) / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b)))
q_vec, c_vecs = embed([query])[0], embed(corpus)
ranked = sorted(zip(corpus, [cos(q_vec, v) for v in c_vecs]), key=lambda x: -x[1])
for text, score in ranked[:3]:
print(f"[{score:.3f}] {text}")
// semantic_search.js — embed a query, rank a small corpus by cosine similarity
// The Node SDK does not yet wrap embeddings, so this template uses fetch directly.
// 1. node 18+
// 2. export NORTHGATE_API_KEY=ngk_your_key_here
// 3. node semantic_search.js "your search query"
const apiKey = process.env.NORTHGATE_API_KEY;
const baseUrl = "https://northgate-ai.polsia.app/v1";
const query = process.argv[2] || "how do I rotate my API key?";
const corpus = [
"POST /auth/key/regenerate-session rotates your API key; the old key is invalidated the moment a new one is issued.",
"NorthGate enforces per-key rate limits: 100/day (developer), 10k/month (team), 100k/month (enterprise).",
"Streaming responses arrive as Server-Sent Events; each line is a chunk you can append to the assistant message.",
"Embeddings use POST /v1/embeddings and return a float vector per input; cosine similarity ranks candidates.",
"Audit logs never store raw prompts — only token counts, latency, route, and a SHA-256 hash of the client IP.",
];
function cos(a, b) {
let dot = 0, na = 0, nb = 0;
for (let i = 0; i < a.length; i++) { dot += a[i] * b[i]; na += a[i] * a[i]; nb += b[i] * b[i]; }
return dot / (Math.sqrt(na) * Math.sqrt(nb));
}
(async () => {
const r = await fetch(baseUrl + "/embeddings", {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": apiKey },
body: JSON.stringify({ model: "text-embedding-3-small", input: [query, ...corpus] }),
});
const { data } = await r.json();
const vecs = data.map(d => d.embedding);
const qVec = vecs[0], cVecs = vecs.slice(1);
const ranked = corpus.map((text, i) => ({ text, score: cos(qVec, cVecs[i]) }))
.sort((a, b) => b.score - a.score)
.slice(0, 3);
ranked.forEach(m => console.log("[" + m.score.toFixed(3) + "] " + m.text));
})();
# Run it
pip install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
python semantic-search.py
npm install northgate-sdk
export NORTHGATE_API_KEY=ngk_your_key_here
node semantic-search.js
Streaming Chat
PythonNode.js
Streams a chat completion via Server-Sent Events. The SDK does not yet stream, so this template uses fetch directly.
# streaming_chat.py — stream tokens as they arrive from the gateway
# The Python SDK does not yet expose a streaming helper, so we read SSE directly.
# 1. pip install requests (stdlib urllib also works — see semantic_search.py)
# 2. export NORTHGATE_API_KEY=ngk_your_key_here
# 3. python streaming_chat.py
import os, json, urllib.request, sys
api_key = os.environ["NORTHGATE_API_KEY"]
url = "https://northgate-ai.polsia.app/v1/chat/completions?route=balanced"
payload = json.dumps({
"model": "meta-llama/Llama-3.3-70B-Instruct",
"messages": [{"role": "user", "content": "Write a haiku about Canadian data residency."}],
"stream": True,
}).encode()
req = urllib.request.Request(url, data=payload, headers={
"Content-Type": "application/json",
"X-API-Key": api_key,
"Accept": "text/event-stream",
}, method="POST")
with urllib.request.urlopen(req, timeout=60) as resp:
print("assistant> ", end="", flush=True)
for line in resp:
line = line.decode("utf-8").strip()
if not line.startswith("data: "):
continue
chunk = line[6:]
if chunk == "[DONE]":
print("\n"); break
try:
delta = json.loads(chunk)["choices"][0]["delta"].get("content")
except (KeyError, json.JSONDecodeError):
continue
if delta:
print(delta, end="", flush=True)
// streaming_chat.js — stream tokens as they arrive from the gateway
// The Node SDK does not yet expose a streaming helper, so we read SSE directly.
// 1. node 18+
// 2. export NORTHGATE_API_KEY=ngk_your_key_here
// 3. node streaming_chat.js
(async () => {
const apiKey = process.env.NORTHGATE_API_KEY;
const url = "https://northgate-ai.polsia.app/v1/chat/completions?route=balanced";
const r = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": apiKey, "Accept": "text/event-stream" },
body: JSON.stringify({
model: "meta-llama/Llama-3.3-70B-Instruct",
messages: [{ role: "user", content: "Write a haiku about Canadian data residency." }],
stream: true,
}),
});
process.stdout.write("assistant> ");
const reader = r.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let idx;
while ((idx = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, idx).trim();
buffer = buffer.slice(idx + 1);
if (!line.startsWith("data: ")) continue;
const chunk = line.slice(6);
if (chunk === "[DONE]") { console.log("\n"); return; }
try {
const delta = JSON.parse(chunk).choices[0].delta.content || "";
process.stdout.write(delta);
} catch (_) { /* skip malformed */ }
}
}
})();