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

# Set A/B test

> Create an A/B test, or save its next version.

What an A/B test does is explained in [A/B tests](https://docs.weflayr.com/product/ab-tests.md).

> [!WARNING]
>
> Changes live routing: the AI gateway splits the calls that send the A/B test's `strategy/<slug>` between its variants as soon as it is saved.

## Parameters

Passed in the request body.

**user\_email** `string`

The email of the member of your organization recorded as the version's author. Defaults to the person who created the API key, or to the project's creator for a key without one. The response's `recorded_for_email` says which email was recorded.

**document** `object` *required*

The A/B test. Each target in it is a routing strategy of the project that is not archived, `{"type": "strategy", "strategy_slug", "version"}` (`version` null for whichever version is live when the call arrives, a number for that version), or one deployment, `{"type": "model", "model", "provider", "region"}`, named as a gateway request names it: the AI gateway's [`GET /v1/models`](https://docs.weflayr.com/api/gateway/models) lists them.

**document**

**name** `string` *required*

The A/B test's name.

**slug** `string`

The id calls route with: they send `strategy/<slug>` as their `model`. Letters, digits, `-` and `_`, at most 100 characters, and not the slug of a routing strategy of the project. Omit it on a new A/B test to derive it from the name.

**variants** `object` *required*

`{"variant_a": {"traffic_pct", "target"}, "variant_b": {"traffic_pct", "target"}}`: `traffic_pct` is the share of the calls the variant serves, a whole number from 1 to 99, the two adding up to 100. Each call goes to a variant at random, independently of the calls before it, so the same customer can be served by both. The two targets must differ.

**customer\_exclusion** `object`

`{"excluded_customers": [...], "target": {...}}`: the customers (their `customer_name`) whose calls always go to `target`, outside the test. List customers with [Get customers](https://docs.weflayr.com/api/management/get-customers.md). Omit it to keep no customer out.

**expected\_version** `integer`

Omit it to create an A/B test. To save the next version, pass the test's slug in the document and the version you read as live here: the save is refused with a 409 `stale_version` when another version has gone live since.

The response also carries `blockers` (what prevents the usual next step), in plain words. It is empty when the usual workflow applies.

```python Python theme={null}
import weflayr
from weflayr.openapi.models import SetAbTestInput

client = weflayr.client()  # reads WEFLAYR_API_KEY

client.set_ab_test(body=SetAbTestInput.from_dict({
    "document": {
        "name": "Summarize profile A/B test",
        "slug": "summarize-profile-ab-test",
        "variants": {
            "variant_a": {
                "traffic_pct": 80,
                "target": {
                    "type": "model",
                    "model": "claude-sonnet-5-5",
                    "provider": "anthropic",
                    "region": "GLOBAL",
                },
            },
            "variant_b": {
                "traffic_pct": 20,
                "target": {
                    "type": "strategy",
                    "strategy_slug": "summarize-profile-router",
                },
            },
        },
        "customer_exclusion": {
            "excluded_customers": ["Meridian Health"],
            "target": {
                "type": "model",
                "model": "claude-sonnet-5-5",
                "provider": "anthropic",
                "region": "GLOBAL",
            },
        },
    },
    "user_email": "alex@acme.com",
}))
```

```js Node.js theme={null}
const weflayr = require('weflayr');

const client = weflayr.client(); // reads WEFLAYR_API_KEY

await weflayr.api.setAbTest({
  client,
  throwOnError: true,
  body: {
    document: {
      name: 'Summarize profile A/B test',
      slug: 'summarize-profile-ab-test',
      variants: {
        variantA: {
          trafficPct: 80,
          target: {
            type: 'model',
            model: 'claude-sonnet-5-5',
            provider: 'anthropic',
            region: 'GLOBAL',
          },
        },
        variantB: {
          trafficPct: 20,
          target: {
            type: 'strategy',
            strategySlug: 'summarize-profile-router',
          },
        },
      },
      customerExclusion: {
        excludedCustomers: ['Meridian Health'],
        target: {
          type: 'model',
          model: 'claude-sonnet-5-5',
          provider: 'anthropic',
          region: 'GLOBAL',
        },
      },
    },
    userEmail: 'alex@acme.com',
  },
});
```

```bash cURL theme={null}
curl -X POST https://app.weflayr.com/api/ab-tests/ \
  -H "Authorization: Bearer $WEFLAYR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "document": {
      "name": "Summarize profile A/B test",
      "slug": "summarize-profile-ab-test",
      "variants": {
        "variant_a": {
          "traffic_pct": 80,
          "target": {
            "type": "model",
            "model": "claude-sonnet-5-5",
            "provider": "anthropic",
            "region": "GLOBAL"
          }
        },
        "variant_b": {
          "traffic_pct": 20,
          "target": {
            "type": "strategy",
            "strategy_slug": "summarize-profile-router"
          }
        }
      },
      "customer_exclusion": {
        "excluded_customers": ["Meridian Health"],
        "target": {
          "type": "model",
          "model": "claude-sonnet-5-5",
          "provider": "anthropic",
          "region": "GLOBAL"
        }
      }
    },
    "user_email": "alex@acme.com"
  }'
```

```json 200 theme={null}
{
  "slug": "summarize-profile-ab-test",
  "version": 1,
  "document": {
    "name": "Summarize profile A/B test",
    "slug": "summarize-profile-ab-test",
    "variants": {
      "variant_a": {
        "traffic_pct": 80,
        "target": {
          "model": "claude-sonnet-5-5",
          "provider": "anthropic",
          "region": "GLOBAL",
          "type": "model"
        }
      },
      "variant_b": {
        "traffic_pct": 20,
        "target": {
          "type": "strategy",
          "strategy_slug": "summarize-profile-router",
          "version": null
        }
      }
    },
    "customer_exclusion": {
      "excluded_customers": [
        "Meridian Health"
      ],
      "target": {
        "model": "claude-sonnet-5-5",
        "provider": "anthropic",
        "region": "GLOBAL",
        "type": "model"
      }
    }
  },
  "archived_at": null,
  "updated_at": "2026-10-24T10:00:00+00:00",
  "url": "https://app.weflayr.com/dashboard/ab-tests/?test=summarize-profile-ab-test",
  "recorded_for_email": "alex@acme.com",
  "blockers": []
}
```

```json 400 theme={null}
{"error": "No member of your organization has the email alex@acme.com. Ask the person for the email they use in Weflayr, and send it again.", "code": "unknown_user"}
```

```json 401 theme={null}
{"error": "Invalid or missing API key. Send it as \"Authorization: Bearer <your key>\"."}
```

```json 404 theme={null}
{"error": "No A/B test has the slug 'nope'. List the project's A/B tests, archived ones included, with get_ab_tests (status=all)."}
```

```json 409 theme={null}
{"error": "'summarize-profile-ab-test' is already the id of a routing configuration of this project. Choose another name or slug: a slug stays taken by an archived A/B test or strategy too. To change the existing A/B test, pass its slug in the document and the version you read as live in expected_version.", "code": "slug_taken"}
```

```json 422 theme={null}
{"error": "Invalid request payload.", "detail": [{"field": "document", "error": "Value error, the project has no live version of the routing strategy 'summarize-profile-router-v2'"}]}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.