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

# Custom API

> Connect any AI agent via API

## Overview

The Custom API integration allows you to connect any AI agent that follows our API specifications. This enables you to integrate your own AI solutions or third-party services with Pactory.

## Prerequisites

* An API endpoint that accepts POST requests
* API authentication credentials (if required)
* API response format matching our specifications

## API Specifications

### Request Format

<CodeGroup>
  ```json Headers theme={null}
  {
    "Content-Type": "application/json",
    "api-key": "your-api-key"
  }
  ```

  ```json Body theme={null}
  {
    "messages": [
      {
        "role": "user",
        "content": "User message"
      },
      {
        "role": "assistant",
        "content": "Previous assistant response"
      },
      {
        "role": "user",
        "content": "Current user message"
      }
    ],
    "userId": "user12345",
    "redirectUrl": "https://dashboard.pactory.ai/chat/agentname/sessionId"
  }
  ```
</CodeGroup>

### Response Format

```json theme={null}
{
  "choices": [{
    "message": {
      "content": "Agent's response text"
    }
  }],
  "usage": {
    "prompt_tokens": 123,    // Optional
    "completion_tokens": 456  // Optional
  }
}
```

<Note>
  If token counts are not provided in the response, we will calculate them using the [GPT-4 tiktoken encoding](https://github.com/openai/tiktoken). This ensures accurate billing even when the API doesn't provide token counts.
</Note>

## Integration Steps

<Steps>
  <Step title="Access Integration">
    From your Pactory dashboard, go to "Add New Agent" and select "Custom API"
  </Step>

  <Step title="Configure Basic Settings">
    Fill in the [standard agent configuration fields](/features/agent-connection) (name, description, etc.)
  </Step>

  <Step title="Configure Integration">
    Enter your API configuration:

    * API endpoint URL (required)
    * API key (required)
  </Step>

  <Step title="Test Connection">
    Send a test message to verify the integration is working properly
  </Step>

  <Step title="Share your agent and get paid based on usage!" />
</Steps>
