POST

/api/v1/chat

The Chat API allows you to create conversational AI interactions with support for multi-turn conversations, system prompts, and conversation history persistence. It's perfect for building chatbots, virtual assistants, and interactive AI applications.

Examples

Here's how to use the Chat API with different programming languages and our official SDKs:

// Basic chat completion
const response = await fetch('https://api.bleujs.org/v1/chat', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  },
  body: JSON.stringify({
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Hello! How can you help me today?' }
    ],
    model: 'gpt-4',
    max_tokens: 1000,
    temperature: 0.7
  })
});

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

// Multi-turn conversation with history
const conversationId = 'conv_123456';
const response = await fetch('https://api.bleujs.org/v1/chat', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  },
  body: JSON.stringify({
    messages: [
      { role: 'user', content: 'What is the capital of France?' }
    ],
    conversation_id: conversationId,
    model: 'gpt-4',
    max_tokens: 500
  })
});

Request & Response

POSThttps://api.bleujs.org/v1/chat

Parameters

ParameterTypeRequiredDescription
messagesarrayYesArray of message objects with role and content
modelstringNoAI model to use (default: gpt-4)
max_tokensintegerNoMaximum tokens in response (default: 1000)
temperaturefloatNoRandomness of response (0-2, default: 0.7)
conversation_idstringNoID for conversation persistence
system_promptstringNoSystem message to set behavior

Message Object

FieldTypeRequiredDescription
rolestringYessystem, user, or assistant
contentstringYesMessage content
namestringNoName of the message author

Response Format

{
  "id": "chat_1234567890",
  "object": "chat.completion",
  "created": 1677649963,
  "model": "gpt-4",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! I'm here to help you with any questions or tasks you might have."
      },
      "index": 0,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 15,
    "total_tokens": 40
  },
  "provider": "openai",
  "latency": 1250,
  "cost": 0.0008
}

Rate Limits

Plan-based Limits

Trial Plan

  • • 500 max tokens
  • • 10 max messages
  • • 1,000 max prompt length

Core Plan

  • • 2,000 max tokens
  • • 50 max messages
  • • 4,000 max prompt length

Enterprise Plan

  • • 4,000 max tokens
  • • 100 max messages
  • • 8,000 max prompt length

Best Practices

💡 Conversation Management

  • • Use conversation_id for persistent conversations
  • • Keep conversation history manageable (under 50 messages for core plan)
  • • Use system prompts to set consistent behavior
  • • Implement conversation cleanup for long-running chats

🚀 Performance Optimization

  • • Set appropriate max_tokens to control costs
  • • Use lower temperature for factual responses
  • • Implement client-side caching for repeated questions
  • • Monitor response times and adjust model selection

🔒 Security & Privacy

  • • Never send sensitive data in messages
  • • Implement user authentication and authorization
  • • Use conversation IDs to isolate user data
  • • Regularly rotate API keys

Error Handling

Common Error Codes

400Bad Request - Invalid parameters or plan limits exceeded
401Unauthorized - Invalid or missing API key
429Too Many Requests - Rate limit exceeded
500Internal Server Error - Service temporarily unavailable