Skip to content

API Examples (Python & JavaScript)

This page provides quick, copy-paste examples for common API flows.

External API v1 (Python)

import os
import requests

BASE = os.getenv('BASE_URL', 'http://localhost:8002/api/v1')
API_KEY = os.getenv('EXTERNAL_API_KEY', 'dev-123')

resp = requests.post(
    f"{BASE}/content/generate",
    headers={'X-API-Key': API_KEY, 'Content-Type': 'application/json'},
    json={'type': 'seo_article', 'businessId': 'dev-biz', 'keywords': ['mulch', 'landscaping'], 'llmProvider': 'ollama'}
)
print(resp.status_code, resp.json())

External API v1 (JavaScript)

import fetch from 'node-fetch'

const BASE = process.env.BASE_URL || 'http://localhost:8002/api/v1'
const API_KEY = process.env.EXTERNAL_API_KEY || 'dev-123'

const res = await fetch(`${BASE}/content/generate`, {
  method: 'POST',
  headers: { 'X-API-Key': API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({ type: 'seo_article', businessId: 'dev-biz', keywords: ['mulch','landscaping'], llmProvider: 'ollama' })
})
console.log(await res.json())

CPM (Python)

import os
import httpx

CPM = os.getenv('CPM_URL', 'http://localhost:8000')
KEY = os.getenv('CPM_API_KEY', 'cpm_test_abc')

async with httpx.AsyncClient() as client:
    r = await client.post(
        f"{CPM}/generate",
        headers={'Authorization': f'Bearer {KEY}'},
        json={'content_type': 'blog', 'prompt': 'Write about mulch benefits', 'client_id': 'dev-client', 'llm_provider': 'ollama'}
    )
    print(r.json())

Frontend API (Next.js)

const res = await fetch('/api/content/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    client_id: '00000000-0000-0000-0000-000000000000',
    content_type: 'blog',
    topic: 'Landscaping mulch tips',
    keywords: ['mulch','landscaping'],
    llm_provider: 'ollama'
  })
})
const data = await res.json()
console.log(data)