Code Examples

Code Examples

Ready-to-use code snippets for popular programming languages and frameworks.

JavaScript / TypeScript

Examples for Node.js, browsers, and TypeScript projects.

JavaScript (Fetch API)

const response = await fetch('https://api.nextcraftai.com/v1/chat', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'google/gemini-2.5-flash',
    messages: [
      { role: 'user', prompt: 'Hello, world!' }
    ]
  })
});

const data = await response.json();
console.log(data.choices[0].message.content);

TypeScript (with types)

interface ChatResponse {
  id: string;
  choices: Array<{
    message: {
      content: string;
    };
  }>;
}

async function chat(prompt: string): Promise<string> {
  const response = await fetch('https://api.nextcraftai.com/v1/chat', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'google/gemini-2.5-flash',
      messages: [{ role: 'user', prompt }]
    })
  });

  if (!response.ok) {
    throw new Error(`API error: ${response.status}`);
  }

  const data: ChatResponse = await response.json();
  return data.choices[0].message.content;
}

Python

Examples using the requests library.

import requests

response = requests.post(
    'https://api.nextcraftai.com/v1/chat',
    headers={
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json',
    },
    json={
        'model': 'google/gemini-2.5-flash',
        'messages': [
            {'role': 'user', 'prompt': 'Hello, world!'}
        ]
    }
)

data = response.json()
print(data['choices'][0]['message']['content'])

cURL

Command-line examples for testing and debugging.

curl -X POST https://api.nextcraftai.com/v1/chat \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemini-2.5-flash",
    "messages": [
      {
        "role": "user",
        "prompt": "Hello, world!"
      }
    ]
  }'