# products.coupon_save

Criar ou editar cupom. Código sem espaços, único na conta (sem diferenciar maiúsculas). `discountValue`: 1 a 100 para `percentage`; centavos para `fixed`. `offerIds` nulo vale para todas as ofertas. O Atomiks não tem checkout: o cupom fica guardado, sem aplicar desconto a pagamento.

- Endpoint: `POST https://app.atomiks.com.br/api/v1/products.coupon_save` (corpo JSON)
- Quem pode chamar: administrador da conta
- Escopo da chave: `crm:write`
- Grava dados: sim
- Pela chave de API: sim
- Ferramenta MCP: `products_coupon_save`
- Ficha em JSON: `https://app.atomiks.com.br/api/v1/openapi.json` (operationId `products.coupon_save`)

## Entrada

| Campo | Tipo | Obrigatório | Nota |
|---|---|---|---|
| `productId` | string (uuid) | sim |  |
| `id` | string (uuid) | não |  |
| `code` | string | sim |  |
| `discountType` | "percentage" \| "fixed" | sim |  |
| `discountValue` | integer | sim |  |
| `maxUses` | integer \| null | não |  |
| `startsAt` | string (date-time) \| null | não |  |
| `endsAt` | string (date-time) \| null | não |  |
| `offerIds` | string (uuid)[] \| null | não |  |
| `audience` | "everyone" \| "email_list" \| "offer_buyers" | não |  |
| `allowedEmails` | string (email)[] | não |  |
| `requiredOfferIds` | string (uuid)[] | não |  |
| `requiredMatch` | "any" \| "all" | não |  |

<details><summary>JSON Schema</summary>

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "productId": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
    },
    "id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
    },
    "code": {
      "type": "string",
      "minLength": 2,
      "maxLength": 40,
      "pattern": "^\\S+$"
    },
    "discountType": {
      "type": "string",
      "enum": [
        "percentage",
        "fixed"
      ]
    },
    "discountValue": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "maxUses": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        {
          "type": "null"
        }
      ]
    },
    "startsAt": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
        },
        {
          "type": "null"
        }
      ]
    },
    "endsAt": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
        },
        {
          "type": "null"
        }
      ]
    },
    "offerIds": {
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string",
            "format": "uuid",
            "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "audience": {
      "type": "string",
      "enum": [
        "everyone",
        "email_list",
        "offer_buyers"
      ]
    },
    "allowedEmails": {
      "maxItems": 500,
      "type": "array",
      "items": {
        "type": "string",
        "format": "email",
        "pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"
      }
    },
    "requiredOfferIds": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "uuid",
        "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
      }
    },
    "requiredMatch": {
      "type": "string",
      "enum": [
        "any",
        "all"
      ]
    }
  },
  "required": [
    "productId",
    "code",
    "discountType",
    "discountValue"
  ]
}
```

</details>

## Exemplo

```bash
curl -X POST https://app.atomiks.com.br/api/v1/products.coupon_save \
  -H "Authorization: Bearer $ATOMIKS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"productId":"00000000-0000-0000-0000-000000000000","code":"DESCONTO10","discountType":"percentage","discountValue":10}'
```

CLI:

```bash
node cli/atomiks.mjs products.coupon_save --json '{"productId":"00000000-0000-0000-0000-000000000000","code":"DESCONTO10","discountType":"percentage","discountValue":10}'
```

MCP: ferramenta `products_coupon_save` com a mesma entrada.

## Saída

Sucesso: `{"data": ...}` com o formato abaixo.

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "productId": {
      "type": "string"
    },
    "code": {
      "type": "string"
    },
    "discountType": {
      "type": "string",
      "enum": [
        "percentage",
        "fixed"
      ]
    },
    "discountValue": {
      "type": "number"
    },
    "active": {
      "type": "boolean"
    },
    "maxUses": {
      "type": [
        "number",
        "null"
      ]
    },
    "usedCount": {
      "type": "number"
    },
    "startsAt": {
      "type": [
        "string",
        "null"
      ]
    },
    "endsAt": {
      "type": [
        "string",
        "null"
      ]
    },
    "offerIds": {
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "audience": {
      "type": "string",
      "enum": [
        "everyone",
        "email_list",
        "offer_buyers"
      ]
    },
    "allowedEmails": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "requiredOfferIds": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "requiredMatch": {
      "type": "string",
      "enum": [
        "any",
        "all"
      ]
    }
  },
  "required": [
    "id",
    "productId",
    "code",
    "discountType",
    "discountValue",
    "active",
    "maxUses",
    "usedCount",
    "startsAt",
    "endsAt",
    "offerIds",
    "audience",
    "allowedEmails",
    "requiredOfferIds",
    "requiredMatch"
  ],
  "additionalProperties": false
}
```

## Erros

`{"error": {"code", "message", "requestId"}}`. Códigos: `invalid_input` (400), `unauthorized` (401), `forbidden` / `account_pending` / `scope_missing` (403), `not_found` (404), `limit_reached` (409), `internal` (500).
