# Create an order subscription

To [create](https://docs.commercelayer.io/core/creating-resources) a new order subscription, send a `POST` request to the `/api/order_subscriptions` endpoint, passing the resource arguments in the request body.

## Request

**POST** <https://yourdomain.commercelayer.io/api/order\\_subscriptions>

### Arguments

| Body Parameter                             | Type       | Required                                     |
| ------------------------------------------ | ---------- | -------------------------------------------- |
| **type**                                   | `string`   | Required                                     |
| attributes.**frequency**                   | `string`   | Required                                     |
| attributes.**activate\_by\_source\_order** | `boolean`  | Optional, default is 'true'                  |
| attributes.**place\_target\_order**        | `boolean`  | Optional, default is 'true'                  |
| attributes.**renewal\_alert\_period**      | `integer`  | Optional, default is '1'                     |
| attributes.**starts\_at**                  | `datetime` | Required, unless activate\_by\_source\_order |
| attributes.**expires\_at**                 | `datetime` | Optional                                     |
| attributes.**reference**                   | `string`   | Optional                                     |
| attributes.**reference\_origin**           | `string`   | Optional                                     |
| attributes.**metadata**                    | `object`   | Optional                                     |
| relationships.**market**                   | `object`   | Optional                                     |
| relationships.**source\_order**            | `object`   | Required                                     |
| relationships.**tags**                     | `array`    | Optional                                     |

### Example

{% tabs %}
{% tab title="Request" %}
The following request creates a new order subscription:

```shell
curl -g -X POST \
  'https://yourdomain.commercelayer.io/api/order_subscriptions' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
  "data": {
    "type": "order_subscriptions",
    "attributes": {
      "frequency": "monthly",
      "starts_at": "2018-01-01T12:00:00.000Z"
    },
    "relationships": {
      "source_order": {
        "data": {
          "type": "orders",
          "id": "ABCRtyUpBa"
        }
      }
    }
  }
}'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `201 Created` status code, returning the created resource object:

```json
{
  "data": {
    "id": "XAyRWNUzyN",
    "type": "order_subscriptions",
    "links": {
      "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN"
    },
    "attributes": {
      "number": "1234",
      "status": "draft",
      "frequency": "monthly",
      "activate_by_source_order": true,
      "place_target_order": true,
      "renewal_alert_period": 1,
      "customer_email": "john@example.com",
      "starts_at": "2018-01-01T12:00:00.000Z",
      "expires_at": "2018-01-02T12:00:00.000Z",
      "last_run_at": "2018-01-01T12:00:00.000Z",
      "next_run_at": "2018-01-01T12:00:00.000Z",
      "occurrencies": 2,
      "errors_count": 3,
      "succeeded_on_last_run": true,
      "created_at": "2018-01-01T12:00:00.000Z",
      "updated_at": "2018-01-01T12:00:00.000Z",
      "reference": "ANY-EXTERNAL-REFEFERNCE",
      "reference_origin": "ANY-EXTERNAL-REFEFERNCE-ORIGIN",
      "metadata": {
        "foo": "bar"
      }
    },
    "relationships": {
      "market": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/market",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/market"
        }
      },
      "subscription_model": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/subscription_model",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/subscription_model"
        }
      },
      "source_order": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/source_order",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/source_order"
        }
      },
      "customer": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/customer",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/customer"
        }
      },
      "customer_payment_source": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/customer_payment_source",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/customer_payment_source"
        }
      },
      "order_subscription_items": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/order_subscription_items",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/order_subscription_items"
        }
      },
      "order_factories": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/order_factories",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/order_factories"
        }
      },
      "recurring_order_copies": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/recurring_order_copies",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/recurring_order_copies"
        }
      },
      "orders": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/orders",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/orders"
        }
      },
      "events": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/events",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/events"
        }
      },
      "tags": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/tags",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/tags"
        }
      },
      "versions": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/versions",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/versions"
        }
      },
      "event_stores": {
        "links": {
          "self": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/relationships/event_stores",
          "related": "https://yourdomain.commercelayer.io/api/order_subscriptions/XAyRWNUzyN/event_stores"
        }
      }
    },
    "meta": {
      "mode": "test",
      "organization_id": "xRRkjDFafe",
      "trace_id": "69abaa3545913c78132e5578bd26208d44aa9043647d78698fd0021f3958cd74"
    }
  }
}
```

{% endtab %}
{% endtabs %}
