> For the complete documentation index, see [llms.txt](https://flagship.gitbook.io/flagship/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flagship.gitbook.io/flagship/master/decision-api.md). Agents can also search and read this documentation through its [MCP server](https://flagship.gitbook.io/flagship/~gitbook/mcp).

# Decision API

Learn how to use our Decision API

Welcome to the Flagship Decision API documentation!

You can use our API to access the Decision API endpoints, which triggers visitor assignment to your campaigns and return the campaign variation.

We have language bindings in Shell (Curl) and Javascript (ES6).

## Campaign

### Run all Campaigns assignment

```
curl -X POST \
  https://decision-api.flagship.io/v1/<ENVIRONMENT_ID>/campaigns \
  -H 'Content-Type: application/json' \
  -d '{
        "visitor_id": "YOUR_VISITOR_ID",
        "context": {
            "YOUR KEY": "YOUR VALUE"
        },
        "decision_group": null
    }'
```

```javascript
const response = await axios.post("%apiurl%/<ENVIRONMENT_ID>/campaigns", {
  visitor_id: "YOUR_VISITOR_ID",
  context: {
    "YOUR KEY": "YOUR VALUE"
  },
  decision_group: null
});
```

This endpoint retrieve all the campaigns affected to your visitor and the context (key/value pairs). It returns a JSON structured like this:

```javascript
{
  "visitorId": "YOUR_VISITOR_ID",
  "campaigns": [
    {
      "id": "<CAMPAIGN_ID>",
      "variationGroupId": "<VARIATION_GROUP_ID>",
      "variation": {
        "id": "<VARIATION_ID>",
        "modifications": {
          "type": "JSON",
          "value": {
            "key": "value"
          }
        }
      }
    }
  ]
}
```

By default, the API will send a hit to trigger a campaign assignment event for the visitor ID and each affected campaign.

### HTTP Request

`POST %apiurl%/<ENVIRONMENT_ID>/campaigns`

### Route Parameters

| Parameter       | Required | Default | Description                                                                                                                |
| --------------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| ENVIRONMENT\_ID | Yes      |         | The environment ID identifes your account and environment (you can find it in your environment quickstart in the platform) |

### Query Parameters

| Parameter | Required | Default | Description                                                               |
| --------- | -------- | ------- | ------------------------------------------------------------------------- |
| mode      | No       | normal  | The mode of the response body you want to receive \[mode]\(made the link) |

### Body Parameters

| Parameter       | Type    | Required | Default | Description                                                                                                                   |
| --------------- | ------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- |
| visitor\_id     | string  | Yes      |         | Your visitor's ID (can be a unique ID from your database or a session ID)                                                     |
| trigger\_hit    | boolean | No       | true    | Should the visitor be affected to targeted campaigns                                                                          |
| context         | object  | Yes      |         | JSON object of key/value pairs picturing the visitor and environment context                                                  |
| decision\_group | string  | No       | empty   | If specified, visitors that matches targeting will be affected to a unique variation ID per \[decision group]\(make the link) |

$plugin\[{ "type": "callout", "data": { "text": "The visitor\_id should be a unique identifier for your visitor. This fields is what guarantees that **the same visitor** will always see **the same variation** of a campaign", "type": "info", "title": "Info" } }]$

### Response parameters

| Parameter           | Type   | Description                                                                                        |
| ------------------- | ------ | -------------------------------------------------------------------------------------------------- |
| visitorId           | string | (only if mode = normal or full) The visitor ID as you sent it in the request                       |
| campaigns           | array  | (only if mode = normal or full) an array of all the affected campaigns                             |
| campaignsVariation  | array  | (only if mode = simple or full) an array of campaign ID and the assigned variation ID              |
| mergedModifications | string | (only if mode = simple or full) A key/value object of all the merged modifications of the campaign |

## Run a single Campaign assignment

$plugin\[{ "type": "code-block", "data": { "languageBlocks": \[ { "code": "curl -X POST \\\n %apiurl%\\/\\/campaigns\\/ \\\n -H 'Content-Type: application\\/json' \\\n -d '{\n \\"visitor\_id\\": \\"YOUR\_VISITOR\_ID\\",\n \\"context\\": {\n \\"YOUR KEY\\": \\"YOUR VALUE\\"\n },\n \\/\\/ Optional : see #\\/decision\_groups for details\n \\"decision\_group\\": null\n }'", "language": "bash" }, { "code": "const response = await axios.post(\n \\"%apiurl%\\/\\/campaigns\\/\\",\n {\n visitor\_id: \\"YOUR\_VISITOR\_ID\\",\n context: {\n \\"YOUR KEY\\": \\"YOUR VALUE\\"\n },\n \\/\\/ Optional : see #\\/decision\_groups for details\n decision\_group: null\n }\n);", "language": "javascript" } ] } }]$

This endpoint retrieves the affection of your visitor ID with a specific context (key/value pairs) to the specified campaign ID. It returns a JSON structured like this:

$plugin\[{ "type": "code-block", "data": { "languageBlocks": \[ { "code": "{\n \\"id\\": \\"\\",\n \\"variationGroupId\\": \\"\\",\n \\"variation\\": {\n \\"id\\": \\"\\",\n \\"modifications\\": {\n \\"type\\": \\"JSON\\",\n \\"value\\": {\n \\"key\\": \\"value\\"\n }\n }\n }\n}", "language": "json" } ] } }]$

### HTTP Request

`POST %apiurl%/<ENVIRONMENT`*`ID>/campaigns/<CAMPAIGN`*`ID>`

You can also make a GET request, passing every `body` parameter as a query parameter with the same format.

This can be useful if you want to include a campaign with a REDIRECT modification type into a newsletter for instance.

`GET %apiurl%/<ENVIRONMENT_ID>/campaigns/<CAMPAIGN_ID>?visitor_id=<visitor_id>&trigger_hit=<trigger_hit>&format_response=<format_response>&context=<context>&default_redirect_url=<default_redirect_url>`

### Query Parameters

| Parameter       | Required | Default | Description                                                                                                                 |
| --------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------- |
| ENVIRONMENT\_ID | Yes      |         | The environment ID identifies your account and environment (you can find it in your environment quickstart in the platform) |
| CAMPAIGN\_ID    | Yes      |         | The campaign ID you want the visitor to be checked for assignment                                                           |

### Body Parameters

| Parameter              | Type    | Required | Default | Description                                                                                                                                              |
| ---------------------- | ------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| visitor\_id            | string  | Yes      |         | Your visitor's ID (can be a unique ID from your database or a session ID)                                                                                |
| trigger\_hit           | boolean | No       | true    | Should the visitor be affected to targeted campaigns                                                                                                     |
| context                | object  | Yes      |         | JSON object of key/value pairs picturing the visitor and environment context                                                                             |
| decision\_group        | string  | No       | empty   | If specified, visitors that matches targeting will be affected to a unique variation ID per \[decision group]\(make the link)                            |
| format\_response       | boolean | No       | false   | If set to true, the response of the API call will be formatted according to your modification. \[See formatted response]\(make the link).                |
| default\_redirect\_url | string  | No       | empty   | If specified, the API call will always return a 302 redirect, with the modification redirection if defined, or the default\_redirect\_url if not defined |

$plugin\[{ "type": "callout", "data": { "text": "The visitor\_id should be a unique identifier for your visitor. This fields is what guarantees that **the same visitor** will always see **the same variation** of a campaign", "type": "info", "title": "Info" } }]$

### Response Parameters

This API endpoint returns a Campaign response **(**[**See campaign response model**](broken://pages/-M35Q3NX2D04RcMdPOHm#campaign)**)**

## Campaign Activation

$plugin\[{ "type": "code-block", "data": { "languageBlocks": \[ { "code": "curl -X POST \\\n \\"%apiurl%\\/activate\\" \\\n -H 'Content-Type: application\\/json' \\\n -d '{\n \\"vid\\": \\"\\",\n \\"cid\\": \\"\\",\n \\"caid\\": \\"\\",\n \\"vaid\\": \\"\\"\n }'", "language": "bash" }, { "code": "const response = await axios.post(\\"%apiurl%\\/activate\\", {\n vid: \\"\\",\n cid: \\"\\",\n caid: \\"\\",\n vaid: \\"\\"\n});", "language": "javascript" } ] } }]$

This endpoint affects a visitor to a variation. It should be used to manually activate a single campaign and variation in case you do not automatically activate them when running campaign assignment **(**[**See trigger\_hit parameter**](broken://pages/-M35Q3NX2D04RcMdPOHm#trigger-hit)**)**.

It returns a `204 HTTP response`.

***Example:***

You create a campaign and you want to target visitors who are on the basket screen of your mobile app.

At the initialisation of your app, you want to get all campaigns and associated modifications. But as you are not on the basket screen you don't want to assign the visitor to a variation yet.

Thus, you must call this endpoint to assign a visitor to a variation only when he actually belongs to it.

### HTTP Request

`POST %apiurl%/activate`

### Body Parameters

| Parameter | Type   | Required | Default | Description                                                               |
| --------- | ------ | -------- | ------- | ------------------------------------------------------------------------- |
| vid       | string | Yes      | empty   | Your visitor's ID (can be a unique ID from your database or a session ID) |
| cid       | string | Yes      | empty   | Your account environment ID (you can find it in your environment settings |
| caid      | string | Yes      | empty   | It correspond to the variationGroupID returned by the decision API.       |
| vaid      | string | Yes      | empty   | The variation ID of which the user will be affected to.                   |
