> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chatbotplatform.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrations

> Connect your bot to AI backends

Integrations connect your bots to AI backends that generate responses. An integration is a webhook configuration that defines how to communicate with services like OpenAI, Anthropic, or your own custom API.

<Frame caption="Empty integrations page">
  <img src="https://mintcdn.com/chatbotplatform/QhGUbEN05MzAjt2K/images/screenshots/02-integrations/01_integrations_empty.png?fit=max&auto=format&n=QhGUbEN05MzAjt2K&q=85&s=aa16a3bbe06e350694b4b49183ed6293" alt="Integrations page" width="1920" height="941" data-path="images/screenshots/02-integrations/01_integrations_empty.png" />
</Frame>

## Overview

An integration consists of:

* **Webhook URL**: The endpoint to send messages to
* **HTTP Headers**: Authentication and custom headers
* **Timeout**: Maximum wait time for responses
* **Weight**: For A/B testing (optional)
* **Payload Format**: The request structure sent to your backend

<Frame caption="Create integration form">
  <img src="https://mintcdn.com/chatbotplatform/oGBHbCo1_YwbpmWy/images/screenshots/02-integrations/02_create_integration_form.png?fit=max&auto=format&n=oGBHbCo1_YwbpmWy&q=85&s=210a0408e49076ce82e28d1047065a6c" alt="Create integration" width="1920" height="941" data-path="images/screenshots/02-integrations/02_create_integration_form.png" />
</Frame>

## Creating an Integration

<Steps>
  <Step title="Navigate to Integrations">
    From your bot page, click **Integrations** in the sidebar.
  </Step>

  <Step title="Click Create Integration">
    Click the **Create Integration** button to open the form.
  </Step>

  <Step title="Configure Basic Settings">
    Enter the integration details:

    <Frame caption="Basic integration information">
      <img src="https://mintcdn.com/chatbotplatform/3g1ISN1rzBY15nxY/images/screenshots/02-integrations/03_integration_basic_info.png?fit=max&auto=format&n=3g1ISN1rzBY15nxY&q=85&s=294163784f9a3c04c5152a9dd80ea5db" alt="Basic integration info" width="1920" height="941" data-path="images/screenshots/02-integrations/03_integration_basic_info.png" />
    </Frame>

    * **Name**: Friendly name (e.g., "OpenAI GPT-4")
    * **Webhook URL**: Your AI backend endpoint
    * **Timeout**: Response timeout in seconds (default: 30)
  </Step>

  <Step title="Add Headers (Optional)">
    Add authentication or custom headers:

    <Frame caption="Integration with custom headers">
      <img src="https://mintcdn.com/chatbotplatform/dChquPcU8U3rTZ0O/images/screenshots/02-integrations/04_integration_with_headers.png?fit=max&auto=format&n=dChquPcU8U3rTZ0O&q=85&s=952cbcdb5c930534200dac83bdcf0ed2" alt="Custom headers" width="1920" height="941" data-path="images/screenshots/02-integrations/04_integration_with_headers.png" />
    </Frame>

    Example headers:

    ```
    Authorization: Bearer sk-...
    Content-Type: application/json
    X-Custom-Header: value
    ```
  </Step>

  <Step title="Configure Advanced Settings">
    Set additional options:

    <Frame caption="Advanced integration settings">
      <img src="https://mintcdn.com/chatbotplatform/dChquPcU8U3rTZ0O/images/screenshots/02-integrations/05_integration_advanced_settings.png?fit=max&auto=format&n=dChquPcU8U3rTZ0O&q=85&s=4d7968b6bcd649e4e306d32d0a7de122" alt="Advanced settings" width="1920" height="941" data-path="images/screenshots/02-integrations/05_integration_advanced_settings.png" />
    </Frame>

    * **Weight**: For A/B testing (default: 1)
    * **Fallback**: Mark as fallback integration
  </Step>

  <Step title="Preview Payload">
    Review the payload format that will be sent:

    <Frame caption="Payload preview">
      <img src="https://mintcdn.com/chatbotplatform/QhGUbEN05MzAjt2K/images/screenshots/02-integrations/06_payload_preview.png?fit=max&auto=format&n=QhGUbEN05MzAjt2K&q=85&s=326f7efa42fedde87be8bb00e90db310" alt="Payload preview" width="1920" height="941" data-path="images/screenshots/02-integrations/06_payload_preview.png" />
    </Frame>
  </Step>

  <Step title="Save">
    Click **Create** to save the integration.

    <Frame caption="Integration created successfully">
      <img src="https://mintcdn.com/chatbotplatform/QhGUbEN05MzAjt2K/images/screenshots/02-integrations/07_integration_created.png?fit=max&auto=format&n=QhGUbEN05MzAjt2K&q=85&s=c468100c685986d613bff8cf173b916d" alt="Integration created" width="1920" height="941" data-path="images/screenshots/02-integrations/07_integration_created.png" />
    </Frame>
  </Step>
</Steps>

## Request Payload Format

When a user sends a message, your integration receives a POST request with this structure:

```json theme={null}
{
  "messages": [
    {
      "role": "user",
      "content": "Hello, how are you?"
    }
  ],
  "conversation_id": "conv_123",
  "user": {
    "id": "user_456",
    "platform": "telegram",
    "name": "John Doe"
  },
  "bot": {
    "id": "bot_789",
    "name": "Support Bot"
  },
  "metadata": {
    "channel": "telegram",
    "chat_id": "123456789"
  }
}
```

### Messages Array

The `messages` array contains conversation history based on your configured context window:

```json theme={null}
"messages": [
  {
    "role": "user",
    "content": "What's the weather?"
  },
  {
    "role": "assistant",
    "content": "I'll check the weather for you."
  },
  {
    "role": "user",
    "content": "Thanks"
  }
]
```

This follows the OpenAI/Anthropic message format for easy integration.

## Expected Response

Your backend should return JSON with this structure:

```json theme={null}
{
  "content": "The response message to send to the user"
}
```

Or with metadata:

```json theme={null}
{
  "content": "The response message",
  "metadata": {
    "model": "gpt-4",
    "tokens": 150,
    "custom_field": "value"
  }
}
```

The `content` field is required. Everything else is optional and stored for your reference.

## Integration Types

### OpenAI Compatible

For OpenAI API or compatible services:

```
URL: https://api.openai.com/v1/chat/completions
Headers:
  Authorization: Bearer YOUR_API_KEY
  Content-Type: application/json
```

You'll need to transform the payload or use a proxy that accepts our format.

### Anthropic (Claude)

For Anthropic's API:

```
URL: https://api.anthropic.com/v1/messages
Headers:
  x-api-key: YOUR_API_KEY
  anthropic-version: 2023-06-01
```

Use a proxy to transform our payload to Anthropic's format.

### Custom Backend

For your own API:

```
URL: https://your-domain.com/api/chat
Headers:
  Authorization: Bearer YOUR_TOKEN
  X-Custom: value
```

Your backend receives our payload format and returns content as JSON.

## Fallback Integrations

Mark an integration as "fallback" to use it when the primary integration fails:

<Frame caption="Fallback integration configuration">
  <img src="https://mintcdn.com/chatbotplatform/0mN6wehDmfoGG8-P/images/screenshots/02-integrations/08_fallback_integration.png?fit=max&auto=format&n=0mN6wehDmfoGG8-P&q=85&s=8e2542b680fe42611456abaf3217d627" alt="Fallback integration" width="1920" height="941" data-path="images/screenshots/02-integrations/08_fallback_integration.png" />
</Frame>

**Fallback behavior**:

1. Bot tries primary integration (highest weight)
2. If it times out or errors, tries fallback
3. If fallback also fails, sends error message to user

This provides reliability for production bots.

## Related Topics

<CardGroup cols={2}>
  <Card title="Webhook Setup" icon="webhook" href="/bots/integrations/webhook-setup">
    Detailed webhook configuration guide
  </Card>

  <Card title="A/B Testing" icon="flask" href="/bots/integrations/ab-testing">
    Compare multiple AI backends
  </Card>

  <Card title="Custom Headers" icon="key" href="/bots/integrations/custom-headers">
    Authentication and custom headers
  </Card>
</CardGroup>

## Testing Your Integration

Use the built-in test feature:

<Steps>
  <Step title="Go to Integration">
    Open the integration you want to test.
  </Step>

  <Step title="Click Test">
    Click the **Test** button.
  </Step>

  <Step title="Send Test Message">
    Enter a test message and submit.
  </Step>

  <Step title="Review Response">
    Check the response from your backend. Look for errors or unexpected behavior.
  </Step>
</Steps>

## Troubleshooting

### Timeout Errors

If requests timeout:

* Increase timeout value
* Optimize your backend for faster responses
* Check network connectivity

### Authentication Errors

If you get 401/403 errors:

* Verify API keys are correct
* Check header format
* Ensure headers include required authentication

### Invalid Response Format

If the bot can't parse responses:

* Ensure response is valid JSON
* Include required `content` field
* Check for encoding issues

## API Access

Manage integrations programmatically using the API.

<AccordionGroup>
  <Accordion title="Create an integration via API">
    <Tabs>
      <Tab title="cURL">
        ```bash theme={null}
        curl -X POST https://api.chatbotplatform.io/v1/bots/bot_123/integrations \
          -H "Authorization: Bearer YOUR_API_KEY" \
          -H "Content-Type: application/json" \
          -d '{
            "name": "OpenAI GPT-4",
            "webhook_url": "https://api.openai.com/v1/chat/completions",
            "headers": {
              "Authorization": "Bearer sk-...",
              "Content-Type": "application/json"
            },
            "timeout": 30,
            "weight": 1
          }'
        ```
      </Tab>

      <Tab title="JavaScript">
        ```javascript theme={null}
        const response = await fetch('https://api.chatbotplatform.io/v1/bots/bot_123/integrations', {
          method: 'POST',
          headers: {
            'Authorization': 'Bearer YOUR_API_KEY',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            name: 'OpenAI GPT-4',
            webhook_url: 'https://api.openai.com/v1/chat/completions',
            headers: {
              'Authorization': 'Bearer sk-...',
              'Content-Type': 'application/json'
            },
            timeout: 30,
            weight: 1
          })
        });
        const data = await response.json();
        ```
      </Tab>

      <Tab title="PHP">
        ```php theme={null}
        $response = Http::withToken('YOUR_API_KEY')
            ->post('https://api.chatbotplatform.io/v1/bots/bot_123/integrations', [
                'name' => 'OpenAI GPT-4',
                'webhook_url' => 'https://api.openai.com/v1/chat/completions',
                'headers' => [
                    'Authorization' => 'Bearer sk-...',
                    'Content-Type' => 'application/json'
                ],
                'timeout' => 30,
                'weight' => 1
            ]);

        $integration = $response->json();
        ```
      </Tab>

      <Tab title="Python">
        ```python theme={null}
        response = requests.post(
            'https://api.chatbotplatform.io/v1/bots/bot_123/integrations',
            headers={'Authorization': 'Bearer YOUR_API_KEY'},
            json={
                'name': 'OpenAI GPT-4',
                'webhook_url': 'https://api.openai.com/v1/chat/completions',
                'headers': {
                    'Authorization': 'Bearer sk-...',
                    'Content-Type': 'application/json'
                },
                'timeout': 30,
                'weight': 1
            }
        )
        integration = response.json()
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="List bot integrations">
    <Tabs>
      <Tab title="cURL">
        ```bash theme={null}
        curl https://api.chatbotplatform.io/v1/bots/bot_123/integrations \
          -H "Authorization: Bearer YOUR_API_KEY"
        ```
      </Tab>

      <Tab title="JavaScript">
        ```javascript theme={null}
        const response = await fetch('https://api.chatbotplatform.io/v1/bots/bot_123/integrations', {
          headers: {
            'Authorization': 'Bearer YOUR_API_KEY'
          }
        });
        const { data } = await response.json();
        ```
      </Tab>

      <Tab title="PHP">
        ```php theme={null}
        $response = Http::withToken('YOUR_API_KEY')
            ->get('https://api.chatbotplatform.io/v1/bots/bot_123/integrations');

        $integrations = $response->json('data');
        ```
      </Tab>

      <Tab title="Python">
        ```python theme={null}
        response = requests.get(
            'https://api.chatbotplatform.io/v1/bots/bot_123/integrations',
            headers={'Authorization': 'Bearer YOUR_API_KEY'}
        )
        integrations = response.json()['data']
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Update an integration">
    <Tabs>
      <Tab title="cURL">
        ```bash theme={null}
        curl -X PATCH https://api.chatbotplatform.io/v1/integrations/int_123 \
          -H "Authorization: Bearer YOUR_API_KEY" \
          -H "Content-Type: application/json" \
          -d '{
            "timeout": 60,
            "weight": 2
          }'
        ```
      </Tab>

      <Tab title="JavaScript">
        ```javascript theme={null}
        await fetch('https://api.chatbotplatform.io/v1/integrations/int_123', {
          method: 'PATCH',
          headers: {
            'Authorization': 'Bearer YOUR_API_KEY',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            timeout: 60,
            weight: 2
          })
        });
        ```
      </Tab>

      <Tab title="PHP">
        ```php theme={null}
        Http::withToken('YOUR_API_KEY')
            ->patch('https://api.chatbotplatform.io/v1/integrations/int_123', [
                'timeout' => 60,
                'weight' => 2
            ]);
        ```
      </Tab>

      <Tab title="Python">
        ```python theme={null}
        requests.patch(
            'https://api.chatbotplatform.io/v1/integrations/int_123',
            headers={'Authorization': 'Bearer YOUR_API_KEY'},
            json={'timeout': 60, 'weight': 2}
        )
        ```
      </Tab>
    </Tabs>
  </Accordion>
</AccordionGroup>

<Card title="Complete API Reference" icon="code" href="https://app.chatbotplatform.io/openapi">
  View the full API specification with all endpoints, parameters, and response schemas.
</Card>

## Next Steps

<CardGroup cols={2}>
  <Card title="Webhook Setup Guide" icon="webhook" href="/bots/integrations/webhook-setup">
    Detailed setup instructions
  </Card>

  <Card title="A/B Testing" icon="flask" href="/bots/integrations/ab-testing">
    Configure multiple integrations
  </Card>
</CardGroup>
