TARQAAI DOCS
Getting Started
API Reference
Resources
API v1.0 · Live

TARQAAI
DOCUMENTATION

One API. Every Model. Unified AI access for developers & enterprises.

Base URL
$https://api.tarqaai.com/api

One unified API key to access multiple cutting-edge AI models (Gemini 2.5 Pro, Flash, 2.0, and many more) with built-in analytics, real-time streaming, and seamless model switching — no juggling multiple providers.

Quick Start

Get started in minutes

1

Generate API Key

Navigate to the API Keys tab in your dashboard and create a new key. Save it securely — you'll only see it once!

Go to Dashboard
2

Make Your First Request

Use curl or your favourite HTTP client to test the API with your authentication key.

Terminal
curl -X POST "https://api.tarqaai.com/api/v1/ask?model=model_name" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello, Tarqa!"}'
3

Start Building

Explore our endpoints, integrate with your application, and monitor usage in real-time.

Authentication

Secure your API requests

All API requests require authentication using an API key passed via HTTP header.

Recommended

HTTP
Authorization: Bearer YOUR_API_KEY

Alternative

HTTP
X-API-Key: YOUR_API_KEY
Security Best Practice
Never expose API keys in client-side code. Use environment variables and make requests from your backend.

Endpoints Overview

All available API endpoints at a glance

POST/api/v1/ask?model=model_namePopular

Simple way to ask a question with a single request

POST/api/v1/chat/completions

OpenAI-compatible chat completions (advanced — supports system messages, multi-turn)

POST/api/v1/chat/completions

Streaming AI chat endpoint for real-time responses — set `"stream": true` in request body

POST/api/v1/context/conversations/createRAG

Create conversation for RAG knowledge base

POST/api/v1/rag/indexRAG

Index documents into vector database for semantic search

POST/api/v1/rag/searchRAG

Semantic search across indexed documents with relevance scoring

POST/api/v1/rag/chatRAG

Chat with AI using indexed documents as context

GET/api/v1/rag/stats/:conversationIdRAG

Get statistics for RAG collection

GET/api/v1/keys/:id/analyticsNew

Get detailed analytics for a specific API key

GET/api/v1/keys

List all API keys for authenticated user

POST/api/v1/keys

Generate a new API key

DELETE/api/v1/keys/:id

Delete an API key

GET/api/health

Health check endpoint for monitoring

Chat API

Stream conversational AI responses with support for multiple models

Available Models

Gemini 2.5 Flash

Fastest with advanced multimodal capabilities

Gemini 2.5 Pro

Most advanced model for complex reasoning

Gemini 2.0 Flash

Fast Gemini 2.0 for general tasks

POSThttps://api.tarqaai.com/api/v1/chat/completions
Request Body
{
  "model": "gemini-2.5-flash",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Explain quantum mechanics."
    }
  ],
  "stream": true
}
Features
Real-time streaming · Automatic usage tracking & analytics · Switch models without changing code · Conversation history support

RAG System

Beta

Retrieval-Augmented Generation — AI that talks to your data

What is RAG?
RAG combines information retrieval with AI generation. Your documents are indexed, semantically searched, and used as context for AI responses — enabling accurate, source-backed answers from your own data.

How RAG Works

1
Index Documents

Upload and chunk your documents into searchable segments with vector embeddings.

2
Semantic Search

Find relevant information using meaning-based search with relevance scoring.

3
AI Chat

AI generates responses using retrieved context from your documents.

RAG Workflow

Step 1: Create Conversation
POST/api/v1/context/conversations/create
Request
{
  "title": "Product Documentation Q&A",
  "knowledgeBaseType": "custom"
}
Step 2: Index Documents
POST/api/v1/rag/index
Request
{
  "conversationId": "conv_abc123",
  "documentText": "TarqaAI is a unified AI platform...",
  "metadata": {
    "source": "product-docs",
    "category": "features"
  }
}
Step 3: RAG Chat
POST/api/v1/rag/chat
Request
{
  "conversationId": "conv_abc123",
  "message": "What AI models does TarqaAI support?",
  "model": "gemini-2.5-flash",
  "topK": 3
}

API Key Analytics

Track real-time usage metrics

Usage Metrics
Total requests
Success rate
Failed requests
Average response time
Model Usage
Models accessed
Request count per model
Usage percentage
Model switching patterns
GET/api/v1/keys/:keyId/analytics?range=30d

Code Generation

Generate production-ready code using AI

AI-Powered Code Generation
Use the Chat API with specialized prompts to generate code in any programming language. Perfect for scaffolding, boilerplate, and rapid prototyping.

Example: Generate React Component

Request to /api/v1/chat/completions
{
  "model": "gemini-2.5-flash",
  "messages": [
    {
      "role": "system",
      "content": "You are an expert React developer. Generate clean, production-ready code."
    },
    {
      "role": "user",
      "content": "Create a responsive login form component with email, password fields, and form validation."
    }
  ]
}

Supported Languages

JavaScript
Python
TypeScript
Java
Go
Rust
PHP
Ruby
Best Practices
Be specific in your prompts (framework versions, style preferences) · Use system messages to set coding standards · Request comments and documentation · Ask for error handling and edge cases

Document Q&A

Ask questions about your documents using RAG

What is Document Q&A?
Upload documents (PDF, DOCX, TXT) and ask questions about them. The AI uses RAG to find relevant sections and generate accurate answers with source citations.

Use Cases

Legal Document Analysis

Extract key terms from contracts, analyze legal documents, find specific clauses.

Research Paper Review

Summarize findings, extract methodology, compare multiple papers.

Knowledge Base Search

Build searchable documentation, internal wikis, customer support systems.

Medical Report Analysis

Extract diagnoses, medications, treatment plans from medical documents.

Complete Workflow

1

Create Conversation

BASH
curl -X POST https://api.tarqaai.com/api/v1/context/conversations/create \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Legal Contract Analysis",
    "knowledgeBaseType": "custom"
  }'
2

Index Your Document

BASH
curl -X POST https://api.tarqaai.com/api/v1/rag/index \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "conversationId": "conv_abc123",
    "documentText": "Your full document text here...",
    "metadata": {
      "filename": "contract.pdf",
      "uploadDate": "2025-12-07"
    }
  }'
3

Ask Questions

BASH
curl -X POST https://api.tarqaai.com/api/v1/rag/chat \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "conversationId": "conv_abc123",
    "message": "What are the payment terms in this contract?",
    "model": "gemini-2.5-flash",
    "topK": 5
  }'

Code Examples

Real-world integration examples for common use cases

Customer Support Chatbot

Real-time streaming chat with message history

React Component
import { useState } from 'react';

function SupportChatbot() {
  const [messages, setMessages] = useState([]);
  const [input, setInput] = useState('');
  const [loading, setLoading] = useState(false);

  const sendMessage = async () => {
    if (!input.trim()) return;

    const userMessage = { role: 'user', content: input };
    setMessages(prev => [...prev, userMessage]);
    setInput('');
    setLoading(true);

    try {
      const response = await fetch(`${BASE_URL}/v1/chat/completions`, {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          model: 'gemini-2.5-flash',
          messages: [...messages, userMessage],
          stream: false
        })
      });

      const data = await response.json();
      const aiMessage = {
        role: 'assistant',
        content: data.choices[0].message.content
      };

      setMessages(prev => [...prev, aiMessage]);
    } catch (error) {
      logger.error('Chat error:', error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="chat-container">
      <div className="messages">
        {messages.map((msg, i) => (
          <div key={i} className={`message ${msg.role}`}>
            {msg.content}
          </div>
        ))}
      </div>
      <input
        value={input}
        onChange={(e) => setInput(e.target.value)}
        onKeyPress={(e) => e.key === 'Enter' && sendMessage()}
        disabled={loading}
      />
    </div>
  );
}

Document Analysis Tool

Upload PDFs and ask questions using RAG

Python Backend
from tarqa import TarqaAI
import PyPDF2

client = TarqaAI(api_key="sk-your-api-key")

def analyze_document(pdf_path, question):
    # Extract text from PDF
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        document_text = ""
        for page in reader.pages:
            document_text += page.extract_text()

    # Create conversation
    conversation = client.context.create_conversation(
        title="Document Analysis",
        knowledge_base_type="custom"
    )

    # Index document
    client.rag.index(
        conversation_id=conversation['id'],
        document_text=document_text,
        metadata={"filename": pdf_path}
    )

    # Ask question
    response = client.rag.chat(
        conversation_id=conversation['id'],
        message=question,
        model="gemini-2.5-flash",
        top_k=5
    )

    return response['answer']

# Usage
answer = analyze_document(
    "contract.pdf",
    "What are the termination clauses?"
)
print(answer)

Multi-Language Translator

Real-time translation using streaming responses

Use Chat API with system prompt for translation

Sales Email Generator

Generate personalized sales emails at scale

Batch requests with different contexts

Content Moderation

Automatically flag inappropriate content

Classification with structured prompts

Data Extraction

Extract structured data from unstructured text

JSON output formatting with prompts

SDKs & Libraries

Official SDKs for seamless integration

JS

JavaScript / TypeScript SDK

For Node.js, React, Vue, Next.js, and more

Installation
BASH
npm install @tarqa/sdk
Quick Start
TS
import { TarqaAI } from '@tarqa/sdk';

const client = new TarqaAI({
  apiKey: process.env.TARQA_API_KEY
});

// Chat Completion
const response = await client.chat.create({
  model: 'gemini-2.5-flash',
  messages: [
    { role: 'user', content: 'Hello!' }
  ]
});

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

// Streaming
for await (const chunk of client.chat.stream({
  model: 'gemini-2.5-flash',
  messages: [{ role: 'user', content: 'Tell me a story' }]
})) {
  process.stdout.write(chunk);
}

// RAG
const conversation = await client.context.createConversation({
  title: 'My Knowledge Base'
});

await client.rag.index({
  conversationId: conversation.id,
  documentText: 'Your document content...'
});

const answer = await client.rag.chat({
  conversationId: conversation.id,
  message: 'What does the document say?'
});
🐍

Python SDK

For Flask, FastAPI, Django, and data science

Installation
BASH
pip install tarqa-sdk
Quick Start
PY
from tarqa import TarqaAI, ChatCompletionRequest, Message

client = TarqaAI(api_key="sk-your-api-key")

# Chat Completion
response = client.chat.create(
    ChatCompletionRequest(
        model="gemini-2.5-flash",
        messages=[
            Message(role="user", content="Hello!")
        ]
    )
)

print(response.choices[0].message.content)

# Streaming
for chunk in client.chat.stream(
    ChatCompletionRequest(
        model="gemini-2.5-flash",
        messages=[Message(role="user", content="Tell me a story")]
    )
):
    print(chunk, end="", flush=True)

# RAG
conversation = client.context.create_conversation(
    title="My Knowledge Base",
    knowledge_base_type="custom"
)

client.rag.index(
    conversation_id=conversation['id'],
    document_text="Your document content..."
)

answer = client.rag.chat(
    conversation_id=conversation['id'],
    message="What does the document say?",
    model="gemini-2.5-flash"
)

Framework Integration Guides

Next.js
Server-side rendering with API routes
⚛️
React
Client-side chat applications
💚
Vue.js
Progressive web applications
🚂
Express.js
RESTful API backends
FastAPI
High-performance Python APIs
🎸
Django
Full-stack Python applications
🌶️
Flask
Lightweight Python microservices
🔥
Svelte
Reactive frontend applications

Rate Limits

Understand and handle API rate limits

Fair Usage
Rate limits ensure fair usage and system stability. All plans include generous limits with automatic retry handling.

Rate Limit Tiers

PlanRequests/MinRequests/DayConcurrent
Free
201,0002
Pro
10010,00010
Enterprise
CustomUnlimitedUnlimited

Rate Limit Headers

Every API response includes rate limit information:

HTTP
HTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1701964800

Handling Rate Limits

Exponential Backoff Implementation
async function makeRequestWithRetry(url, options, maxRetries = 5) {
  let retries = 0;

  while (retries < maxRetries) {
    try {
      const response = await fetch(url, options);

      if (response.status === 429) {
        // Rate limit exceeded
        const waitTime = Math.min(
          Math.pow(2, retries) * 1000, // Exponential backoff
          60000 // Max 60 seconds
        );

        console.log(`Rate limited. Waiting ${waitTime}ms...`);
        await new Promise(resolve => setTimeout(resolve, waitTime));
        retries++;
        continue;
      }

      return response;
    } catch (error) {
      if (retries === maxRetries - 1) throw error;
      retries++;
    }
  }

  throw new Error('Max retries exceeded');
}

// Usage
const response = await makeRequestWithRetry(
  `${BASE_URL}/v1/chat/completions`,
  {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gemini-2.5-flash',
      messages: [{ role: 'user', content: 'Hello!' }]
    })
  }
);
Best Practices
Implement exponential backoff for retries · Monitor rate limit headers proactively · Cache responses when possible · Use batch requests for multiple operations · Contact support for higher limits

Error Handling

Understand and handle API errors gracefully

Error Response Format

JSON
{
  "success": false,
  "error": "Invalid API key",
  "code": "INVALID_API_KEY",
  "type": "authentication_error",
  "meta": {
    "timestamp": "2025-12-07T10:30:00Z",
    "requestId": "req_abc123def456"
  }
}

Common Error Codes

401
INVALID_API_KEY

API key is missing, malformed, or invalid

429
RATE_LIMIT_EXCEEDED

Too many requests — see rate limits section

402
INSUFFICIENT_CREDITS

Account has run out of credits

503
MODEL_NOT_AVAILABLE

Requested model is temporarily unavailable

400
INVALID_REQUEST

Request body is malformed or missing required fields

400
CONTEXT_LENGTH_EXCEEDED

Message exceeds model context window

404
CONVERSATION_NOT_FOUND

RAG conversation ID does not exist

500
INTERNAL_ERROR

Internal server error — please retry

Error Handling Example

JS
async function handleTarqaRequest(apiKey, requestData) {
  try {
    const response = await fetch(`${BASE_URL}/v1/chat/completions`, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${apiKey}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(requestData)
    });

    const data = await response.json();

    if (!response.ok || !data.success) {
      const error = data.error || 'Unknown error';
      const code = data.code || 'UNKNOWN_ERROR';

      switch (code) {
        case 'RATE_LIMIT_EXCEEDED':
          const retryAfter = response.headers.get('Retry-After') || 60;
          await new Promise(r => setTimeout(r, retryAfter * 1000));
          return handleTarqaRequest(apiKey, requestData);

        case 'INVALID_API_KEY':
          throw new Error('Authentication failed. Please check your API key.');

        case 'INSUFFICIENT_CREDITS':
          throw new Error('Account out of credits. Please upgrade your plan.');

        case 'CONTEXT_LENGTH_EXCEEDED':
          requestData.messages = truncateMessages(requestData.messages);
          return handleTarqaRequest(apiKey, requestData);

        case 'INTERNAL_ERROR':
          await new Promise(r => setTimeout(r, 5000));
          return handleTarqaRequest(apiKey, requestData);

        default:
          throw new Error(`API Error: ${error} (Code: ${code})`);
      }
    }

    return data;
  } catch (error) {
    console.error('Request failed:', error);
    throw error;
  }
}

// Usage
try {
  const result = await handleTarqaRequest('sk-your-api-key', {
    model: 'gemini-2.5-flash',
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  console.log(result.choices[0].message.content);
} catch (error) {
  showErrorNotification(error.message);
}
Debugging Tips
Always log the requestId for support · Check response status codes first · Validate request format before sending · Monitor error rates in analytics
Production Best Practices
Implement comprehensive error handling · Use retry logic with exponential backoff · Set up error monitoring (Sentry) · Provide user-friendly error messages

© 2025 TarqaAI · All rights reserved