# Turn A/B testing on or off

`POST https://api.versionseven.ai/v1/campaigns/{campaign_id}/ab-testing`

Turns the campaign's A/B test on or off. `traffic_split` is the percentage of new leads that get `variation_a`, from 0 to 100; the rest get `variation_b`. It applies to leads enrolled from now on: leads already enrolled keep their arm.

Turning the test on needs a `variation_b` with steps, written with [Replace a campaign's sequence](https://docs.versionseven.ai/api-reference/campaigns/replace-sequence); without one, the request answers `409 VARIATION_B_MISSING`. With `enabled: false`, every new lead gets variation A.

- Required scope: `campaigns:write`

## Request

**cURL**

```bash
curl -X POST "https://api.versionseven.ai/v1/campaigns/550e8400-e29b-41d4-a716-446655440000/ab-testing" \
  -H "Authorization: Bearer $VICTORIA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "enabled": true,
  "traffic_split": 50
}'
```

**Node.js**

```javascript
const response = await fetch("https://api.versionseven.ai/v1/campaigns/550e8400-e29b-41d4-a716-446655440000/ab-testing", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.VICTORIA_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "enabled": true,
    "traffic_split": 50
  }),
});

const data = await response.json();
```

**Python**

```python
import os

import requests

response = requests.post(
    "https://api.versionseven.ai/v1/campaigns/550e8400-e29b-41d4-a716-446655440000/ab-testing",
    headers={
        "Authorization": f"Bearer {os.environ['VICTORIA_API_KEY']}",
    },
    json={
        "enabled": True,
        "traffic_split": 50,
    },
)
data = response.json()
```

## Headers

- `Authorization` (string, required): `Bearer` followed by a space and your API key, for example `Bearer vk_…`.

## Path parameters

- `campaign_id` (string · uuid, required): ID of the campaign.

## Request body

- `enabled` (boolean, required): true runs the test; false sends every new lead variation A
- `traffic_split` (integer, optional, ≥ 0, ≤ 100, default `50`): Percentage of NEW leads that get variation A (the rest get B)

## Response

### `200`

```json
{
  "message": "string",
  "ab_testing_enabled": true,
  "campaign_id": "550e8400-e29b-41d4-a716-446655440000",
  "success": true,
  "traffic_split": 1,
  "winner": "string"
}
```

- `message` (string, required)
- `ab_testing_enabled` (boolean or null, optional)
- `campaign_id` (string or null, optional)
- `success` (boolean, optional, default `true`)
- `traffic_split` (integer or null, optional)
- `winner` (string or null, optional)

## Errors

Errors share one JSON body: `success`, `error`, `message`, optional `details`, and `request_id`. For example:

```json
{
  "success": false,
  "error": "UNAUTHORIZED",
  "message": "Invalid or inactive API key",
  "request_id": "734d11a1-54d4-414c-9e8d-4e1ada977db4"
}
```

| Status | Code | Meaning |
| --- | --- | --- |
| 400 | `VALIDATION_ERROR` | The request didn't match the endpoint's schema: a missing or malformed field, a bad query parameter, or a value out of range. `details.errors` lists each failing field with its `field`, `message` and `type`. |
| 401 | `UNAUTHORIZED` | The `Authorization` header is missing or malformed, or the API key is unknown or has been deactivated. |
| 401 | `API_KEY_EXPIRED` | The API key is past its expiry date. Create a new key in the Victoria AI app. |
| 403 | `INSUFFICIENT_SCOPE` | The API key doesn't have the scope this endpoint requires, such as `leads:write`. |
| 403 | `ORGANIZATION_DEACTIVATED` | The organization that owns this API key has been deactivated. |
| 404 | `CAMPAIGN_NOT_FOUND` | No campaign with this ID exists in your organization. |
| 404 | `NOT_FOUND` | No endpoint matches the path. A resource ID in the path that isn't a valid UUID also answers `NOT_FOUND`. |
| 409 | `VARIATION_B_MISSING` | The campaign's sequence has no `variation_b` with steps, so there's nothing to test against variation A. Write one with [Replace a campaign's sequence](https://docs.versionseven.ai/api-reference/campaigns/replace-sequence) before turning A/B testing on or promoting a winner. |
| 413 | `PAYLOAD_TOO_LARGE` | The request body is larger than 1 MB. |
| 429 | `RATE_LIMITED` | Too many requests for this API key: more than 100 a minute to one endpoint, or 600 a minute in total. Retry after the number of seconds in the `Retry-After` header. See [Rate limits](https://docs.versionseven.ai/guides/rate-limits). |
| 500 | `INTERNAL_ERROR` | Something failed on our side. The response never includes internal details; quote its `request_id` when you contact support. |
| 503 | `UPSTREAM_TIMEOUT` | A service the API depends on timed out. The request is safe to retry. |
| 503 | `AUTH_UNAVAILABLE` | The API key couldn't be checked because the authentication service was unavailable. The request is safe to retry. |

## Response headers

| Header | Description |
| --- | --- |
| `X-Request-ID` | Correlation ID for the request, also returned as `request_id` in error bodies. Send your own `X-Request-ID`, up to 128 letters, digits, `.`, `_`, `:` or `-`, and it's used instead. |
| `RateLimit-Limit` | Requests allowed to this endpoint per minute. |
| `RateLimit-Remaining` | Requests you can still send to this endpoint right now. |
| `RateLimit-Reset` | Seconds until the endpoint's full limit is available again. |
| `Retry-After` | On a `429`: seconds to wait before retrying. |
