13. API Integration - Programmatic AI

Move beyond chat interfaces and IDE tools - integrate AI directly into your applications via APIs. Build features like AI chatbots, content generation, code analysis, and more.

Major AI APIs for Developers

OpenAI API

GPT-4, GPT-3.5, DALL-E, Whisper, Embeddings

  • Chat Completions: Conversational AI
  • Function Calling: AI triggers your functions
  • Embeddings: Semantic search, RAG
  • Vision: Image understanding
  • Pricing: Pay-per-token ($0.01-0.06 per 1K tokens)

Anthropic API (Claude)

Claude 3.5 Sonnet, Opus, Haiku

  • 200K context for large document processing
  • System prompts: Better instruction following
  • Tool use: Similar to function calling
  • Pricing: Competitive with OpenAI

Google AI (Gemini)

Gemini Pro, Ultra (via Vertex AI)

  • 1M tokens context window
  • Multimodal: Text, images, video, audio
  • Grounding: Google Search integration
  • Pricing: Very competitive

Quick Start Example (OpenAI)

// Node.js
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const response = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [
    { role: "system", content: "You are a helpful coding assistant" },
    { role: "user", content: "Explain async/await in JavaScript" }
  ]
});

console.log(response.choices[0].message.content);

Common Use Cases

  • AI Chatbots: Customer support, internal tools
  • Content Generation: Blog posts, product descriptions
  • Code Analysis: Security scanning, code review
  • Semantic Search: Using embeddings + vector DB
  • Data Extraction: Structured output from unstructured text

Best Practices

  • Rate limiting: Handle API rate limits gracefully
  • Error handling: Retry with exponential backoff
  • Caching: Cache responses when appropriate
  • Cost monitoring: Track token usage to avoid surprises
  • Streaming: Use streaming for better UX (real-time responses)