# Create campaign

POST `https://app.iteras.dk/api/campaigns/createcampaign/`

The API key must allow "Change campaigns".

Creates a campaign, which describes what a subscription receives in a period, how long the period is and how it is invoiced, and which campaign the subscription continues on. [Listing the campaigns](/api/docs/server/campaigns.md) shows how the existing campaigns are set up.

Plans are built out of campaigns. For instance, you can build an intro offer followed by the permanent plan by having a cheaper intro campaign followed by the permanent campaign. The intro campaign has its next campaign set to the permanent campaign, and the permanent campaign has its next campaign set to itself.

## Parameters

- `campaign_id` (string, required): Self-chosen ID for the campaign, e.g. `3months`. Must be unique in the account and must not collide with a product's external ID.
- `name` (string, required): Internal campaign name shown inside Iteras, e.g. `3 months`.
- `customer_facing_name` (string, required): Campaign name shown to customers, e.g. `3 months`.
- `business_entity` (string): External ID of the business entity the campaign belongs to, e.g. `DKbranch`. Can be left out if the account has only one business entity.
- `products` (array, required): The products in the campaign, each an object identifying the product by its `external_id` or, if it has none, its `name`, e.g. `[{"external_id": "example_product"}]`. When the duration of the campaign is based on counting assignments, one of the objects needs `used_for_period_duration` set to true, e.g. `[{"external_id": "example_product", "used_for_period_duration": true}]`. At least one of the account's subscription products is required unless `pro_forma_subscription` is true.

  [Log in](/login/?next=/api/docs/server/createcampaign.md), or send your API key as a bearer token, to see the subscription products set up for your account.
- `duration_based_on` (string, required): How the period length is determined: `time` for calendar time, given in `time`, or `assignments` for counting assignments of products, given in `period_assignments`.
- `time` (string): Length of each period in calendar time, required when `duration_based_on` is `time`: a number followed by `d`, `w`, `m` or `y` for days, weeks, calendar months or calendar years, e.g. `6m`, or `fixed` to end on the date in `fixed_date`.

  Durations are written as:

  - *N*d, e.g. `3d` for 3 days
  - *N*w, e.g. `2w` for 2 weeks
  - *N*m, e.g. `6m` for 6 calendar months, calculated precisely, so not the same as 6×30 days
  - *N*y, e.g. `1y` for 1 calendar year, which handles leap years
- `period_assignments` (integer): Number of assigned products in each period, e.g. set it to 3, and the period ends when 3 products have been assigned to the period. Required when `duration_based_on` is `assignments`.
- `fixed_date` (date): Date the period ends, as `YYYY-MM-DD`, e.g. `2027-03-01`. Required when `time` is `fixed`.
- `next_campaign_id` (string): `campaign_id` of the campaign the subscription continues on, e.g. `3months`. For introduction campaigns, this will usually be the permanent campaign. For permanent recurring campaigns, this will usually be the campaign's own `campaign_id`.
- `repeat_number_of_times` (integer, Defaults to `1`): How many times in a row the subscription gets a period on the campaign before continuing, e.g. 3.
- `policies` (array): External IDs of business policies to optionally override the business entity's settings with, e.g. `["no_pause"]`, as [listed with the business policies](/api/docs/server/policies.md). The first policy in the list that sets a setting decides it.
- `active` (bool, Defaults to `true`): Whether the campaign is active. An inactive campaign isn't offered in ordering forms or for new subscriptions in Iteras, and existing subscriptions continue on it. Once a campaign has been used, you cannot delete it, but you can make it inactive.
- `lines` (array): What to invoice for each period, e.g. `[{"text": "3 issues", "amount": 24950, "tax_rate": 0.25, "extra_info": ["period"]}]`, in the [invoice line format](/api/docs/server/invoicelinesformat.md). No lines means the campaign is free. A price change is planned with dates: the current line gets a `valid_to`, and a copy with the new amount gets the same date as `valid_from`.
- `controlled_group_campaigns` (array): Any recipient campaigns that can be used for recipient subscriptions for a subscription on this campaign, e.g. `[{"campaign_id": "employee", "max_controlled_subscriptions": 5}]`.

  Each object has the following parameters:

  - `campaign_id` (string, required): ID of the recipient campaign, e.g. `employee`.
  - `sync_with_controlling_period` (bool, Defaults to `true`): Synchronize the recipient subscriptions' periods with the controlling subscription's period, so they can be invoiced together.
  - `max_controlled_subscriptions` (integer): Limit how many subscriptions the controlling subscription may have on the campaign, e.g. `5`. No limit if left out.
- `controlled_addon_campaigns` (array): Any add-on campaigns that can be used for add-on subscriptions for a subscription on this campaign, e.g. `[{"campaign_id": "digital-extra", "max_controlled_subscriptions": 5}]`.

  Each object has the following parameters:

  - `campaign_id` (string, required): ID of the add-on campaign, e.g. `digital-extra`.
  - `sync_with_controlling_period` (bool, Defaults to `true`): Synchronize the add-on subscriptions' periods with the base subscription's period, so they can be invoiced together.
  - `max_controlled_subscriptions` (integer): Limit how many subscriptions the controlling subscription may have on the campaign, e.g. `5`. No limit if left out.
- `switching` (object): Campaigns the customer can switch to, grouped by situation, each a list of objects with `campaign_id`, e.g. `{"allowed_from_selfservice": [{"campaign_id": "3months"}]}`.
  - `allowed_from_selfservice`, campaigns the customer can switch to from the self-service
  - `offered_instead_of_cancel`, campaigns offered instead of cancelling in the self-service
  - `offered_from_invoice`, campaigns offered through a link in the invoice
- `pro_forma_subscription` (bool, Defaults to `false`, may change): Whether the subscription is only pro forma for accounting purposes and receives nothing.
- `product_image` (string, may change): Name of an image from the image library in Iteras to show as the campaign's product image, e.g. `cover.jpg`.
- `access_paywall` (bool, Defaults to `true`, may change): Whether an active subscription gets access to content protected by a paywall.
- `access_content` (string, Defaults to `all`, may change): The access to previous products when integrating with digital edition platforms, one of:
  - `""`, no products
  - `received`, assigned products
  - `all`, all products while active plus assigned products
- `unit_price` (integer, may change): In case of customer complaints that warrant crediting, you can specify the unit price in cents including tax, e.g. `150` for €1.50.
- `receives_issues` (bool, Defaults to `true`, may change): For subscriptions where products are periodically assigned, e.g. magazines, whether upcoming products are assigned to the subscription automatically. If false, products are not assigned.

You can set any custom campaign field, using its field id (the field name with a colon prepended, or its external identifier).

[Log in](/login/?next=/api/docs/server/createcampaign.md), or send your API key as a bearer token, to see the custom campaign fields set up for your account.

A yes/no field (type `bool`) is `true` or `false`, and has no value when it isn't set. Set it with `true` or `false`, and clear it with `null`.

## Examples

```http
POST /api/campaigns/createcampaign/ HTTP/1.1
Content-Type: application/json
```

```json
{
  "campaign_id": "3months",
  "name": "3 months",
  "customer_facing_name": "3 months",
  "business_entity": "DKbranch",
  "duration_based_on": "assignments",
  "period_assignments": 3,
  "products": [
    { "external_id": "example_product", "used_for_period_duration": true }
  ],
  "lines": [
    { "text": "3 issues", "amount": 24950, "tax_rate": 0.25 }
  ]
}
```

## Return value

On success the endpoint returns the Iteras ID and the campaign ID of the created campaign:

```json
{ "created": true, "id": "308", "campaign_id": "3months" }
```

## Errors

Invalid JSON returns 400.

A well-formed request with invalid values is not executed and returns 422 with the errors keyed by field, for instance an already-used `campaign_id`:

```json
{ "created": false, "errors": { "campaign_id": ["A campaign with this ID already exists."] } }
```

This also covers needing to specify `business_entity` when the account has several:

```json
{ "created": false, "errors": { "business_entity": ["Must specify \"business_entity\"."] } }
```