AI API Connections
Connect Node.js and TypeScript applications to modern AI APIs using official SDK clients and secure environment authentication.
TL;DR
- Initialize official SDK clients using secure
process.envcredential keys. - Specify explicit
max_tokenslimits to avoid unexpected generation costs. - Extract generated response text from the
choicesorcontentarray.
Client Initialization
OpenAI ClientInstantiate official OpenAI client with environment credentials.
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});Anthropic ClientInitialize Anthropic SDK client reading standard environment keys.
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});Request TimeoutConfigure explicit request timeout using AbortSignal controllers.
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), 15000);
// Pass { signal: ctrl.signal } to requestStandard Message Payloads
OpenAI Chat PayloadSend standard chat messages array with role definitions.
const res = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'Be concise.' },
{ role: 'user', content: 'Define closure.' }
],
max_tokens: 250,
});Anthropic Message PayloadSend Anthropic messages with separate top-level system parameter.
const res = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 250,
system: 'Be concise.',
messages: [{ role: 'user', content: 'Define DNS' }]
});Temperature SettingTune randomness between deterministic logic and creative generation.
const deterministic = { temperature: 0.1 };
const creative = { temperature: 0.8 };
// Low temperature preserves factual accuracyResponse Parsing
Extract OpenAI TextRead generated text from first completion choice safely.
const msg = res.choices[0]?.message;
const text = msg?.content ?? '';
const finishReason = res.choices[0]?.finish_reason;Extract Claude TextParse content blocks from Anthropic message response.
const block = res.content[0];
const text = block?.type === 'text'
? block.text
: '';
const stopReason = res.stop_reason;Token Usage AccountingLog input and output token consumption for cost accounting.
const u = res.usage!;
const inTokens = u.prompt_tokens;
const outTokens = u.completion_tokens;
console.log(`Used: ${inTokens} in / ${outTokens} out`);Universal Provider Abstraction
Unified Message TypeDefine portable message interface across all model providers.
interface ChatMessage {
role: 'system' | 'user' | 'assistant';
content: string;
}Adapter InterfaceContract for executing vendor-agnostic chat completions.
interface LLMAdapter {
complete(msgs: ChatMessage[]): Promise<string>;
}Factory ResolverResolve active model provider based on runtime configuration.
function getLLM(p: 'openai' | 'claude'): LLMAdapter {
return p === 'openai'
? new OpenAIAdapter()
: new ClaudeAdapter();
}Tips
- Store secret API credentials inside private
process.envvariables rather than committing raw authentication tokens to version control. - Implement a universal adapter interface to swap between
OpenAIandAnthropicmodels without altering core application logic.
Warnings
- Never expose secret API keys like
OPENAI_API_KEYinside client-side browser code or public single-page script bundles. - Avoid calling AI APIs synchronously without passing an
AbortSignaltimeout to prevent hanging connections indefinitely.
In Practice
Initializes an OpenAI client to dispatch a prompt with timeout controls and prints completion tokens.
- Import official SDK and instantiate client with process.env keys.
- Set up an AbortController timeout signal for network safety.
- Dispatch chat completion with system instructions and token limits.
- Extract generated response text and return completion result.
import OpenAI from 'openai';
const client = new OpenAI();
async function askAI(prompt: string) {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), 10000);
try {
const res = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: prompt }],
max_tokens: 150,
}, { signal: ctrl.signal });
return res.choices[0]?.message?.content ?? '';
} finally {
clearTimeout(t);
}
}
console.log(await askAI('Explain API keys in brief'));FAQ
Store your keys in a local .env file and access them through process.env.OPENAI_API_KEY. In production, inject secrets via platform environment settings or cloud secret managers like AWS Secrets Manager.
The system message sets persistent persona guidelines, domain constraints, and formatting rules. The user message contains dynamic runtime instructions or queries submitted by end users.
Create an instance of AbortController and pass its signal property into the SDK request options. Triggering controller.abort() cancels the underlying HTTP connection immediately.