# API Integration Examples

This section shows how AI providers integrate their models with AI1NET using standard HTTP APIs.

***

### 📡 1. Base Requirements

Your API must expose an endpoint like:

```
POST /v1/generate
```

#### Required Headers

```http
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
```

***

### 🧠 2. Text Model (LLM) Example

#### Request

```json
{
  "input": "Explain quantum computing in simple terms",
  "parameters": {
    "max_tokens": 200,
    "temperature": 0.7
  }
}
```

***

#### Response

```json
{
  "output": "Quantum computing is a type of computation that uses quantum bits...",
  "usage": {
    "input_tokens": 8,
    "output_tokens": 120
  },
  "latency_ms": 320
}
```

***

#### Node.js Example (Provider Side)

```ts
import express from "express"

const app = express()
app.use(express.json())

app.post("/v1/generate", async (req, res) => {
  const { input, parameters } = req.body

  // simulate model output
  const output = `AI response to: ${input}`

  res.json({
    output,
    usage: {
      input_tokens: input.length / 4,
      output_tokens: output.length / 4,
    },
    latency_ms: 120,
  })
})

app.listen(3000, () => {
  console.log("Model API running on port 3000")
})
```

***

### 🎨 3. Image Generation Model

#### Request

```json
{
  "input": "A futuristic AI city, neon lights, cyberpunk",
  "parameters": {
    "size": "1024x1024"
  }
}
```

***

#### Response

```json
{
  "output": {
    "image_url": "https://your-api.com/images/abc123.png"
  },
  "latency_ms": 2100
}
```

***

### 🎤 4. Audio / Voice Model

#### Request

```json
{
  "input": "Hello, welcome to AI1NET",
  "parameters": {
    "voice": "female_en",
    "format": "mp3"
  }
}
```

***

#### Response

```json
{
  "output": {
    "audio_url": "https://your-api.com/audio/output.mp3"
  },
  "latency_ms": 850
}
```

***

### 🔁 5. Streaming (Advanced - Optional)

For real-time responses (chat-like):

#### Response (Chunked)

```json
{
  "delta": "Quantum"
}
```

```json
{
  "delta": " computing"
}
```

```json
{
  "done": true
}
```

***

### ⚙️ 6. Model Registration (AI1NET Side)

When registering your model:

#### Example Config

```json
{
  "name": "MyGPT-1",
  "type": "text",
  "endpoint": "https://api.myai.com/v1/generate",
  "auth_type": "bearer",
  "pricing": {
    "input_per_1k_tokens": 0.002,
    "output_per_1k_tokens": 0.004
  },
  "capabilities": ["chat", "completion", "coding"]
}
```

***

### 🧠 7. Routing Metadata (Important)

To help AI1NET route traffic correctly:

```json
{
  "performance": {
    "avg_latency_ms": 300,
    "accuracy_score": 0.92
  },
  "cost_efficiency": "medium",
  "specialties": ["coding", "technical explanations"]
}
```

***

### 🔒 8. Authentication Options

Supported methods:

#### Bearer Token (Recommended)

```http
Authorization: Bearer sk-xxxxx
```

#### API Key Header

```http
x-api-key: YOUR_KEY
```

***

### 📊 9. Error Handling

#### Example Error Response

```json
{
  "error": {
    "message": "Rate limit exceeded",
    "code": 429
  }
}
```

***

### 💰 10. Usage Reporting (Optional but Powerful)

```json
{
  "usage": {
    "requests": 1200,
    "tokens_processed": 850000
  }
}
```

***

### 🔥 Best Practices

* Keep latency low (<500ms if possible)
* Return consistent JSON format
* Handle errors gracefully
* Provide clear capability metadata
* Optimize for specific tasks (routing advantage)

***

### 🚀 What This Enables

With this API structure:

* Your model becomes **plug-and-play**
* AI1NET can **route traffic automatically**
* You can **earn instantly**
* Developers can **build on top of your model**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ai1net.xyz/for-ai-providers/api-integration-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
