# Create a company

URL: https://docs.sparklayer.io/developers/api/customers/create-a-company
Operation: `POST /api/v2/companies` (operationId `createCompany`)
OpenAPI spec: https://docs.sparklayer.io/openapi/customers.yaml

`POST /api/v2/companies`

**Base URLs:** `https://app.sparklayer.io` (Live), `https://test.app.sparklayer.io` (Test)

**Authentication:** send `Authorization: Bearer <access_token>` and `Site-Id: <site id>` with every request. Get the token from [Get an access token](https://docs.sparklayer.io/developers/api/core/get-an-access-token.md); see [Authentication](https://docs.sparklayer.io/developers/authentication.md).

### Example request

```bash
curl -X POST "https://app.sparklayer.io/api/v2/companies" \
  -H "Authorization: Bearer $SPARKLAYER_TOKEN" \
  -H "Site-Id: $SPARKLAYER_SITE_ID" \
  -H "Content-Type: application/json" \
  -d '{
  "id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
  "external_id": "123456789",
  "integration_id": "123456789",
  "name": "Acme Corp",
  "customer_group": "base",
  "accounting_id": "<accounting_id>",
  "price_lists": [
    "b2b_gb_gbp_123"
  ],
  "payment_on_account": null,
  "default_shipping_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "default_billing_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "allow_personal_addresses": true,
  "allow_one_time_addresses": true,
  "sales_agent_groups": [
    "south-west-uk"
  ],
  "platform_managed_fields": [
    "customer_group"
  ],
  "primary_customer_id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
  "company_admin_ids": [
    "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b"
  ]
}'
```

Set `SPARKLAYER_TOKEN` to an access token and `SPARKLAYER_SITE_ID` to your Site ID. For the test environment, use `https://test.app.sparklayer.io`.

### Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Site-Id` | string | Yes | Your SparkLayer Site ID, from Settings > API in the SparkLayer Dashboard. Length: min 1. Example: `jones-climbing` |

### Request body (`application/json`) (required)

ContextProperties plus the sales agent groups a company or section assigns, and the fields a platform sync manages on it.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string (uuid) | | SparkLayer ID |
| `external_id` | string \| null | | The identifier on the e-commerce platform. Length: min 1, max 124. |
| `integration_id` | string \| null | | The identifier used in another integration. Length: min 1, max 124. |
| `name` | string | | Company name. Length: max 128. |
| `customer_group` | string \| null | | Customer Group Slug. Length: min 1, max 128. |
| `accounting_id` | string \| null | | |
| `price_lists` | string[] \| null | | Section specific price lists (overrides customer group settings) |
| `payment_on_account` | object \| null | | Allow payments on account when the order total meets available credit (overrides customer group settings). Default: `null`. |
| `payment_on_account.credit_limit` | number (float) \| null | | Max 999999999.999. |
| `payment_on_account.balance` | number (float) \| null | | What the customer owes. Sending it adds an entry to the customer's account credit ledger: an opening balance with `balance_date`, otherwise the balance as of now. Max 999999999.999. |
| `payment_on_account.currency_code` | string \| null | | If set allows overriding the currency for the customer, otherwise the base currency of the store is to be used. Pattern: `^[A-Za-z]{3}$`. |
| `payment_on_account.net_terms` | "7_days" \| "15_days" \| "30_days" \| "45_days" \| "60_days" \| "90_days" \| null | | |
| `payment_on_account.balance_date` | string (date-time) \| null | | Makes `balance` an opening balance on this date: from then on, only orders and payments dated on or after it count towards the balance. Leave it out to set the balance as of now. |
| `default_shipping_address_id` | string (uuid) \| null | | |
| `default_billing_address_id` | string (uuid) \| null | | |
| `allow_personal_addresses` | boolean \| null | | |
| `allow_one_time_addresses` | boolean \| null | | |
| `sales_agent_groups` | string[] \| null | | |
| `platform_managed_fields` | ("customer_group" \| "price_lists" \| "payment_on_account" \| "sales_agent_groups" \| "allow_personal_addresses" \| "allow_one_time_addresses" \| "accounting_id")[] \| null | | The fields a platform sync sets here, which are edited on the platform rather than in SparkLayer. Omitted or null on a write leaves them as they are; an empty list means the platform sets none. |
| `primary_customer_id` | string (uuid) | | SparkLayer Customer ID of the Primary for this Company |
| `company_admin_ids` | (string (uuid))[] | | |

```json
{
  "id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
  "external_id": "123456789",
  "integration_id": "123456789",
  "name": "Acme Corp",
  "customer_group": "base",
  "accounting_id": "<accounting_id>",
  "price_lists": [
    "b2b_gb_gbp_123"
  ],
  "payment_on_account": null,
  "default_shipping_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "default_billing_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "allow_personal_addresses": true,
  "allow_one_time_addresses": true,
  "sales_agent_groups": [
    "south-west-uk"
  ],
  "platform_managed_fields": [
    "customer_group"
  ],
  "primary_customer_id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
  "company_admin_ids": [
    "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b"
  ]
}
```

### Responses

#### 201: Created

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string (uuid) | | SparkLayer ID |
| `external_id` | string \| null | | The identifier on the e-commerce platform. Length: min 1, max 124. |
| `integration_id` | string \| null | | The identifier used in another integration. Length: min 1, max 124. |
| `name` | string | | Company name. Length: max 128. |
| `customer_group` | string \| null | | Customer Group Slug. Length: min 1, max 128. |
| `accounting_id` | string \| null | | |
| `price_lists` | string[] \| null | | Section specific price lists (overrides customer group settings) |
| `payment_on_account` | object \| null | | Allow payments on account when the order total meets available credit (overrides customer group settings). Default: `null`. |
| `payment_on_account.credit_limit` | number (float) \| null | | Max 999999999.999. |
| `payment_on_account.balance` | number (float) \| null | | What the customer owes. Sending it adds an entry to the customer's account credit ledger: an opening balance with `balance_date`, otherwise the balance as of now. Max 999999999.999. |
| `payment_on_account.currency_code` | string \| null | | If set allows overriding the currency for the customer, otherwise the base currency of the store is to be used. Pattern: `^[A-Za-z]{3}$`. |
| `payment_on_account.net_terms` | "7_days" \| "15_days" \| "30_days" \| "45_days" \| "60_days" \| "90_days" \| null | | |
| `payment_on_account.balance_date` | string (date-time) \| null | | Makes `balance` an opening balance on this date: from then on, only orders and payments dated on or after it count towards the balance. Leave it out to set the balance as of now. |
| `default_shipping_address_id` | string (uuid) \| null | | |
| `default_billing_address_id` | string (uuid) \| null | | |
| `allow_personal_addresses` | boolean \| null | | |
| `allow_one_time_addresses` | boolean \| null | | |
| `sales_agent_groups` | string[] \| null | | |
| `platform_managed_fields` | ("customer_group" \| "price_lists" \| "payment_on_account" \| "sales_agent_groups" \| "allow_personal_addresses" \| "allow_one_time_addresses" \| "accounting_id")[] \| null | | The fields a platform sync sets here, which are edited on the platform rather than in SparkLayer. Omitted or null on a write leaves them as they are; an empty list means the platform sets none. |
| `primary_customer` | object | | Simple representation of a customer |
| `primary_customer.id` | string (uuid) | | SparkLayer Customer ID |
| `primary_customer.first_name` | string | | First Name. Length: max 128. |
| `primary_customer.last_name` | string | | Last Name. Length: max 128. |
| `primary_customer.email` | string | | Email Address. Length: max 255. |
| `primary_customer.status` | "active" \| "inactive" \| "archived" | | Status - login is allowed only via the `active` status. `archived` marks a former B2B customer (e.g. Shopify b2b tag removed); archived customers are preserved but hidden from listings and their email may be reused. Default: `active`. |
| `current_balance` | number (float) | | Max 999999999.999. |

```json
{
  "id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
  "external_id": "123456789",
  "integration_id": "123456789",
  "name": "Acme Corp",
  "customer_group": "base",
  "accounting_id": "<accounting_id>",
  "price_lists": [
    "b2b_gb_gbp_123"
  ],
  "payment_on_account": null,
  "default_shipping_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "default_billing_address_id": "3f2b8c1e-5d4a-4e6f-9b7c-1a2d3e4f5a6b",
  "allow_personal_addresses": true,
  "allow_one_time_addresses": true,
  "sales_agent_groups": [
    "south-west-uk"
  ],
  "platform_managed_fields": [
    "customer_group"
  ],
  "primary_customer": {
    "id": "9151f21f-43ae-43b4-92f3-f4af67cdf544",
    "first_name": "Tom",
    "last_name": "Jones",
    "email": "Tom.jones@climbing.com",
    "status": "active"
  },
  "current_balance": 0
}
```

#### 409: Conflict - the site syncs its companies from its eCommerce platform, so an API key can't change its companies, sections, company addresses or company roles

`application/problem+json`: the standard error body (below).

#### Other statuses: An error. The body describes the problem: see [Errors](https://docs.sparklayer.io/developers/errors.md).

`application/problem+json`: the standard error body (below).

### Error body

Error responses with a body use this RFC 7807 problem details object. See [Errors](https://docs.sparklayer.io/developers/errors.md) for every status code and which errors to retry.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `detail` | string | | Human-readable summary of the error |
| `status` | integer | | HTTP Status code returned from API |
| `title` | string | | Machine-readable error code |
| `type` | string | | |
| `errors` | object[] | | |
| `errors[].code` | string | | A machine-readable error message |
| `errors[].property` | string | | The offending property, with the full path |
| `errors[].field` | string | | The offending property, stripped of the path |
| `errors[].message` | string | | A human-readable summary of the error |

```json
{
  "detail": "Data Validation Failed",
  "status": 400,
  "title": "invalid-api-request-contents",
  "type": "https://hub.sparklayer.io/tech-docs",
  "errors": [
    {
      "code": "unique-constraint-violation",
      "property": "purchase_identifiers.sparklayer",
      "field": "sparklayer",
      "message": "Each stock level must have a unique stock location and sku combination"
    }
  ]
}
```
