# Tests (/docs/api/agents/tests)

A test lives in one environment of a space. Paths start with `/api/agent/organizations/{organizationId}/spaces/{spaceId}/{environment}/tests`. Reading needs any role; creating, changing, deleting and copying need `editor` or higher.

## Create an A/B test [#create-an-ab-test]

```bash
curl -X POST https://improve.obelism.studio/api/agent/organizations/$ORG/staging/tests \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Hero copy",
    "options": [
      { "name": "Control", "split": 50 },
      { "name": "Bold", "value": "Try it free", "split": 50 }
    ],
    "events": {
      "start": { "event": "page_view", "scope": "/" },
      "conversion": { "event": "sign_up", "scope": "hero" }
    }
  }'
```

| Field          | Default                                                           |
| -------------- | ----------------------------------------------------------------- |
| `name`         | Required                                                          |
| `slug`         | The slugified name. The SDK looks tests up by it                  |
| `type`         | `ab-test`                                                         |
| `state`        | `draft`. Only `active` tests are served                           |
| `audienceId`   | `null`, everyone. Or an [audience](/docs/api/agents/audiences) id |
| `allocation`   | `100`, the percentage of matching visitors in the test            |
| `options`      | Control and Variation, 50/50                                      |
| `defaultValue` | The first option's slug, served to visitors outside the test      |
| `events`       | None. Set them before going live, or there are no results         |

`events` says what counts: `start` is the event that enters a visitor in the funnel, `conversion` is the goal, and `metrics` are optional steps in between. Each is an `{ "event", "scope" }` pair, the same names your code [tracks](/docs/api/analytics).

## Create a multi-variant test [#create-a-multi-variant-test]

A multi-variant test has `variants` instead of `options`: dimensions whose values are combined. Every combination is an option, with an equal split.

```json
{
	"name": "Buy button",
	"type": "multi-variant-test",
	"variants": [
		{ "name": "Colour", "values": ["red", "blue"] },
		{ "name": "Size", "values": ["small", "large"] }
	]
}
```

This serves `red-small`, `red-large`, `blue-small` and `blue-large`. Its `defaultValue` is the first combination.

## Change a test [#change-a-test]

`PATCH .../tests/{testId}` changes the fields you send. To start a test:

```bash
curl -X PATCH https://improve.obelism.studio/api/agent/organizations/$ORG/staging/tests/$TEST \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"state":"active"}'
```

- `options` and `variants` are the full new list. An item with an `id` updates it, one without is added, and items you leave out are deleted.
- Changing `type` needs the new list, for example `variants` to switch to a multi-variant test.
- `events` merges: `start`, `metrics` and `conversion` you leave out keep their value. Changing events recalculates the results.

The whole body is checked before anything is saved, so an invalid field changes nothing.

## Copy tests to another environment [#copy-tests-to-another-environment]

Copies tests with their state, options and events, like Sync in the dashboard. Results are not copied.

```bash
curl -X POST https://improve.obelism.studio/api/agent/organizations/$ORG/staging/tests/sync \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"testIds":["test_01H8XYZ"],"targetEnvironment":"production"}'
```

```json
{ "created": 1, "overwritten": 0 }
```

When a test with the same slug already exists in the target, nothing is copied and the answer is `409 slug_conflict`, with a `conflicts` list. Send `"overwrite": true` to replace them.

## Other operations [#other-operations]

| Method   | Path              | Does                                         |
| -------- | ----------------- | -------------------------------------------- |
| `GET`    | `/tests`          | List the tests, without options and variants |
| `GET`    | `/tests/{testId}` | Get one test with its options or variants    |
| `DELETE` | `/tests/{testId}` | Delete the test and its exposures (`204`)    |
