# API trigger

Trigger your agent directly from you apps or websites with the API trigger.

## Create trigger

* API triggers are created inside **agent projects**.
* Each API trigger must have a **unique name** and **unique key** within the project.
* The trigger key is **case-insensitive** and can be customised to match an identifier meaningful for your application.

> **Note:** Make sure the key is something easy to reference when making API calls.

<figure><img src="https://382411756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fx5Kure6K0P0mTPmH9FQb%2Fuploads%2FN5JWV01OtxiYH2i19Exq%2FScreenshot%202025-10-04%20at%2016.40.24.png?alt=media&#x26;token=8d95acb2-8c48-42b2-acca-4937c9a1b45f" alt=""><figcaption></figcaption></figure>

### Create API key

API triggers are secured by [API keys](https://chathive.app/api_key) that are managed by your organization.

* The API key must have at least the permission `project.trigger`

<div align="left"><figure><img src="https://382411756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fx5Kure6K0P0mTPmH9FQb%2Fuploads%2F84DHNR2K3WyfRXnKiJLN%2FScreenshot%202025-10-04%20at%2016.42.40.png?alt=media&#x26;token=9397d452-b3f1-421d-a7f7-d7c921695dc9" alt="" width="375"><figcaption></figcaption></figure></div>

#### Best practices

* Only grant the API key access to the specific projects it needs.
* Limit the permissions to only what’s required.

## Send a request

To call your trigger, send a **POST** request to its unique URL.

```
https://api.chathive.app/api/v1.0/project/:projectId/trigger/:key
```

This URL can be found in the trigger settings, or construct it using your project ID and trigger key.

Optionally, include a JSON body with instruction and configuration. The body will be passed to your filters.

* Be processed by the trigger’s filters.
* Be passed to your agent as input.

The api key you created must be included in the headers of the post request.

```url
curl -X POST "https://api.chathive.app/api/v1.0/project/:projectId/trigger/:key" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{ "key": "value" }'
```

### Request body

You can include a body with the trigger to give your AI agent more context when executing your request. The following fields are supported:

<table><thead><tr><th width="190.47210693359375">Status</th><th width="174.94775390625">Value</th><th>Description</th></tr></thead><tbody><tr><td>instruction<br></td><td>string | object</td><td>Instruction that will be provided to the agent, can be a string or object</td></tr><tr><td>sessionId<br><br><br></td><td>string<br><br><br></td><td>Unique string generated by you to distinguished conversations. When sessionId changes all previous conversation context will be lost and it will be like a new conversation has started</td></tr><tr><td>environment<br></td><td>string<br></td><td>Identify in the analytics from where the request came. Default: "api"</td></tr><tr><td>language<br></td><td><a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1 language code</a></td><td>The current language of your website visitor. Must be <a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1 </a></td></tr><tr><td>timezone<br><br><br></td><td>string<br><br><br></td><td>Accepted but request timed out before agent completed it's task. Is only returned when <code>wait for response</code> has been enabled and request took to long.</td></tr><tr><td>customer</td><td><a href="../../sdk/attributes#customer-data">customer data</a></td><td>Data to identify the current customer.</td></tr><tr><td>context<br><br></td><td>record&#x3C;string, Boolean | number | string | undefined></td><td>Extra context that should be used by the agent to provide accurate answers. <br></td></tr></tbody></table>

### Response

<table><thead><tr><th width="190.47210693359375">Status</th><th>Description</th></tr></thead><tbody><tr><td>200<br></td><td>Request has been accepted and agent completed it's task.<br>Body will be included with final response.</td></tr><tr><td>202</td><td>Request has been accepted but agent is stil executing the task. Is only returned when  <code>wait for response</code> has been disabled. /</td></tr><tr><td>400</td><td>Event has been dropped. The body doesn't match the filters</td></tr><tr><td>404<br><br></td><td><ul><li>Trigger does not exist</li><li>Trigger has been paused</li><li>Trigger has been deleted</li></ul></td></tr><tr><td>408<br><br><br></td><td>Accepted but request timed out before agent completed it's task. Is only returned when <code>wait for response</code> has been enabled and request took to long.</td></tr></tbody></table>

## Advanced Settings

### Customize trigger key

Customize the trigger key used in the API to clearly indicate which trigger is being executed in your code. Some key guidelines:

* Must be unique within your organization.
* Can only contain the following characters: `a-zA-Z0-9_-`.

<figure><img src="https://382411756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fx5Kure6K0P0mTPmH9FQb%2Fuploads%2F01LA2HBbnRn9aD4qBZ2u%2FScreenshot%202025-10-08%20at%2009.40.07.png?alt=media&#x26;token=145ed05c-0d12-4742-93fd-2ca4a241b54c" alt=""><figcaption></figcaption></figure>

### Ignore duplicate triggers

To prevent executing the same trigger multiple times and waisting AI credits, you can enforce it to only execute triggers with a unique body. Once this is turned on, every trigger that is executed with the same body will be dropped. This will only check triggers that are executed with a body.

<figure><img src="https://382411756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fx5Kure6K0P0mTPmH9FQb%2Fuploads%2F4J2SCgEKAxiNwkUTCvAF%2FScreenshot%202025-10-08%20at%2010.33.40.png?alt=media&#x26;token=83ef8d2f-8a64-4d59-971a-76c2b045c70a" alt=""><figcaption></figcaption></figure>

### Wait for response

For tasks that take a long time (minutes, hours, or days), you can configure the trigger to immediately return a $$(202)$$ success response once the task is accepted.

This way, your server doesn't have to wait for the task to finish.

<figure><img src="https://382411756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fx5Kure6K0P0mTPmH9FQb%2Fuploads%2FRi2FPdEUjCaMqKYn8BRf%2FScreenshot%202025-10-08%20at%2010.37.31.png?alt=media&#x26;token=fa0d37a4-8cfa-438a-b488-a7c0da954bb8" alt=""><figcaption></figcaption></figure>
