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

# Create DiscountCode

> Create a new DiscountCode record.

A **DiscountCode** represents a discount registered in the system. Each discount code belongs to a `Business` (location) and defines a percentage or fixed-amount discount that can be applied to different item types: price plans (tariffs), bookings, products, and/or events.

Use the boolean flags (`DiscountPricePlans`, `DiscountBookings`, `DiscountProducts`, `DiscountEvents`) to control which item categories the discount applies to. For each enabled category, associate the specific eligible items via the corresponding ID list (e.g. `Tariffs`, `ResourceTypes`, `Products`, `EventCategories`). Use the `Added*` and `Removed*` list variants for incremental updates without replacing the full list.

A discount can specify either `DiscountPercentage` (e.g. 10 for 10% off) or `DiscountAmount` (a fixed monetary amount off), but not both.

Discount codes can be assigned to individual customers via `CoworkerDiscountCode`. Availability can be further restricted by validity dates (`ValidFrom`/`ValidTo`), publish window (`PublishFrom`/`PublishTo`), usage caps (`MaxUses`, `MaxUsesPerUser`), audience (`OnlyForContacts`, `OnlyForMembers`), and expiration rules (`ExpirationType`, `ExpiresIn`).

## Authentication

<Note>
  This endpoint requires OAuth2 authentication. Include a valid bearer token in the `Authorization` header.
  The authenticated user must be a full unrestricted administrator or have the **`DiscountCode-Create`** role.
</Note>

## Enums

<Accordion title="eDiscountExpirePeriod">
  | Value | Name  |
  | ----- | ----- |
  | 1     | Day   |
  | 2     | Week  |
  | 3     | Month |
  | 4     | Year  |
</Accordion>

## Request Body

### Required Fields

<ParamField body="BusinessId" type="integer" required>
  Business Id.
</ParamField>

<ParamField body="Code" type="string" required>
  The unique alphanumeric code customers enter to apply the discount.
</ParamField>

<ParamField body="Description" type="string" required>
  Human-readable description of what this discount code is for.
</ParamField>

### Optional Fields

<ParamField body="Active" type="boolean">
  Whether this discount code is currently active and can be redeemed.
</ParamField>

<ParamField body="PublishFrom" type="string">
  Date from which this discount code is visible/published to customers.
</ParamField>

<ParamField body="PublishTo" type="string">
  Date until which this discount code is visible/published to customers.
</ParamField>

<ParamField body="DiscountPercentage" type="number">
  Percentage discount to apply (e.g. 10 for 10% off). Mutually exclusive with DiscountAmount.
</ParamField>

<ParamField body="DiscountAmount" type="number">
  Fixed monetary amount to discount. Mutually exclusive with DiscountPercentage.
</ParamField>

<ParamField body="ReferralDiscount" type="boolean">
  Whether this discount code is used as part of the referral program.
</ParamField>

<ParamField body="DiscountPricePlans" type="boolean">
  Whether this discount can be applied to price plans (tariffs). When true, use Tariffs to restrict to specific plans.
</ParamField>

<ParamField body="Tariffs" type="integer[]">
  Tariffs.
</ParamField>

<ParamField body="DiscountBookings" type="boolean">
  Whether this discount can be applied to resource bookings. When true, use ResourceTypes to restrict to specific resource types.
</ParamField>

<ParamField body="ResourceTypes" type="integer[]">
  Resource Types.
</ParamField>

<ParamField body="DiscountProducts" type="boolean">
  Whether this discount can be applied to products. When true, use Products to restrict to specific products.
</ParamField>

<ParamField body="Products" type="integer[]">
  Products.
</ParamField>

<ParamField body="DiscountEvents" type="boolean">
  Whether this discount can be applied to events. When true, use EventCategories to restrict to specific event categories.
</ParamField>

<ParamField body="EventCategories" type="integer[]">
  Event Categories.
</ParamField>

<ParamField body="MaxUsesPerUser" type="integer">
  Maximum number of times a single customer can redeem this discount code.
</ParamField>

<ParamField body="MaxUses" type="integer">
  Maximum total number of times this discount code can be redeemed across all customers.
</ParamField>

<ParamField body="OnlyForContacts" type="boolean">
  When true, only contacts (non-member customers) can use this discount code.
</ParamField>

<ParamField body="OnlyForMembers" type="boolean">
  When true, only members (customers with an active plan) can use this discount code.
</ParamField>

<ParamField body="ValidFrom" type="string">
  Start date from which this discount code can be redeemed.
</ParamField>

<ParamField body="ValidTo" type="string">
  End date after which this discount code can no longer be redeemed.
</ParamField>

<ParamField body="ExpirationType" type="integer">
  Unit of the expiration period (Day, Week, Month, Year). Used with ExpiresIn to determine when the discount expires after being assigned to a customer. See `eDiscountExpirePeriod?` enum above.
</ParamField>

<ParamField body="ExpiresIn" type="integer">
  Number of ExpirationType periods after assignment until the discount expires for a customer.
</ParamField>

## Code Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST \
    "https://spaces.nexudus.com/api/billing/discountcodes" \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "BusinessId": 0,
      "Code": "",
      "Description": ""
  }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch(
    'https://spaces.nexudus.com/api/billing/discountcodes',
    {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_TOKEN',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        BusinessId: 0,
        Code: '',
        Description: ''
      })
    }
  );

  const data = await response.json();
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://spaces.nexudus.com/api/billing/discountcodes',
      headers={
          'Authorization': 'Bearer YOUR_TOKEN',
          'Content-Type': 'application/json'
      },
      json={
          'BusinessId': 0,
          'Code': '',
          'Description': ''
      }
  )

  data = response.json()
  ```
</CodeGroup>

## Response

### 200

<ResponseField name="Status" type="integer">
  HTTP status code. `200` on success.
</ResponseField>

<ResponseField name="Message" type="string">
  A human-readable message confirming the creation.
</ResponseField>

<ResponseField name="Value" type="object">
  Contains the `Id` of the newly created record.
</ResponseField>

<ResponseField name="WasSuccessful" type="boolean">
  `true` if the discountcode was created successfully.
</ResponseField>

<ResponseField name="Errors" type="array">
  `null` on success.
</ResponseField>

```json Example Response theme={null}
{
  "Status": 200,
  "Message": "DiscountCode was successfully created.",
  "Value": {
    "Id": 87654321
  },
  "OpenInDialog": false,
  "OpenInWindow": false,
  "RedirectURL": null,
  "JavaScript": null,
  "UpdatedOn": "2025-01-15T10:30:00Z",
  "UpdatedBy": "admin@example.com",
  "Errors": null,
  "WasSuccessful": true
}
```

### 400

<ResponseField name="Message" type="string">
  A summary of the validation error(s), in the format `PropertyName: error message`.
</ResponseField>

<ResponseField name="Value" type="any">
  `null` on validation failure.
</ResponseField>

<ResponseField name="Errors" type="object[]">
  Array of validation errors.

  <Expandable>
    <ResponseField name="AttemptedValue" type="any">
      The value that was submitted for the field, or `null` if missing.
    </ResponseField>

    <ResponseField name="Message" type="string">
      The validation error message.
    </ResponseField>

    <ResponseField name="PropertyName" type="string">
      The name of the property that failed validation.
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="WasSuccessful" type="boolean">
  `false` when the request fails validation.
</ResponseField>

```json Example Response theme={null}
{
  "Message": "Code: is a required field",
  "Value": null,
  "Errors": [
    {
      "AttemptedValue": null,
      "Message": "is a required field",
      "PropertyName": "Code"
    }
  ],
  "WasSuccessful": false
}
```
