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

# Create or update a reward

> Create a coupon reward, or change one, with its promotion codes.

Takes one reward in the catalog's shape (see [Discounts and promotion codes](https://docs.integrateledger.com/billing/discounts.md)). A coupon is a percentage (`percentOff`) or a fixed amount in cents (`amountOffCents`) off, `once`, `repeating` for `durationMonths`, or `forever`, on the paid plans `planIds` names (every version of each), or every paid plan with `null`. It never discounts usage or credit packs. `promotionCodes` are the reward's catalog codes: ones left out are deactivated, and codes added with [Add a promotion code](https://docs.integrateledger.com/api-reference/rewards/create-promotion-code.md) are kept.

Changing the amount, duration or plans makes a new Stripe coupon; customers who already have the discount keep its terms, and every active code moves to the new coupon with its remaining uses. An unknown or free plan returns `400 invalid_reward`, and a code already active on another reward `409 promotion_code_taken`. Needs the **Catalog** permission at Write.


## OpenAPI

````yaml openapi.json POST /v1/rewards
openapi: 3.1.0
info:
  title: Ledger API
  version: 0.1.0
servers:
  - url: https://api.integrateledger.com
    description: Hosted Ledger API
security: []
paths:
  /v1/rewards:
    post:
      tags:
        - Rewards
      summary: Create or update a reward
      operationId: writeReward
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RewardInput'
      responses:
        '200':
          description: >-
            The reward, created or updated. A change to its amount, duration or
            plans mints a new Stripe coupon; customers who already have the
            discount keep its terms.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardMutation'
        '400':
          description: >-
            Invalid request (`invalid_request`) or reward (`invalid_reward`: an
            unknown or free plan in `planIds`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '401':
          description: Invalid Ledger API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '403':
          $ref: '#/components/responses/InsufficientPermissions'
        '409':
          description: >-
            The code is already active, on this reward or another
            (`promotion_code_taken`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '429':
          $ref: '#/components/responses/RateLimited'
      security:
        - ApiKey: []
      x-codeSamples:
        - lang: python
          label: Python SDK
          source: >-
            import os

            from integrateledger import ledger


            client = ledger(api_key=os.environ["LEDGER_API_KEY"])

            client.write_reward({"id": "launch20", "name": "Launch: 20% off for
            3 months", "coupon": {"percentOff": 20, "duration": "repeating",
            "durationMonths": 3, "planIds": ["pro"]}, "promotionCodes":
            [{"code": "LAUNCH20"}]})
        - lang: bash
          label: cURL
          source: |-
            curl --request POST 'https://api.integrateledger.com/v1/rewards' \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"id":"launch20","name":"Launch: 20% off for 3 months","coupon":{"percentOff":20,"duration":"repeating","durationMonths":3,"planIds":["pro"]},"promotionCodes":[{"code":"LAUNCH20"}]}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


            const client = new ledger({ apiKey: process.env.LEDGER_API_KEY ?? ""
            });

            await client.writeReward({ id: "launch20", name: "Launch: 20% off
            for 3 months", coupon: { percentOff: 20, duration: "repeating",
            durationMonths: 3, planIds: ["pro"] }, promotionCodes: [{ code:
            "LAUNCH20" }] });
components:
  schemas:
    RewardInput:
      type: object
      properties:
        id:
          type: string
          minLength: 1
          maxLength: 100
          pattern: ^[a-zA-Z0-9_-]+$
          description: >-
            Your ID for the reward: letters, numbers, `_` or `-`. It can't be
            changed.
        name:
          type: string
          minLength: 1
          maxLength: 120
          description: Shown in the dashboard and as the coupon's name in Stripe.
        coupon:
          type: object
          properties:
            percentOff:
              type: number
              exclusiveMinimum: 0
              maximum: 100
              description: Percent off, above 0 and at most 100.
            amountOffCents:
              type: integer
              exclusiveMinimum: 0
              maximum: 2147483647
              description: A fixed amount off each discounted invoice, in US cents.
            duration:
              type: string
              enum:
                - once
                - repeating
                - forever
              description: >-
                `once`: the next invoice. `repeating`: every invoice for
                `durationMonths`. `forever`: until the subscription ends.
            durationMonths:
              type: integer
              minimum: 1
              maximum: 36
              description: '`repeating` only: 1 to 36 months.'
            planIds:
              type:
                - array
                - 'null'
              items:
                type: string
                minLength: 1
                maxLength: 100
              minItems: 1
              description: >-
                The paid plans it discounts, every version of each. Null: every
                paid plan, now and later. Usage (overage) and credit packs are
                never discounted.
          required:
            - duration
            - planIds
          additionalProperties: false
          description: >-
            What comes off: a percentage or an amount, for how long, on which
            plans. Send this or `grant`.
        grant:
          type: object
          properties:
            items:
              type: array
              items:
                $ref: '#/components/schemas/GrantItemInput'
              minItems: 1
              maxItems: 20
          required:
            - items
          additionalProperties: false
          description: >-
            What the customer gets instead: credits or a metered feature's
            units, granted when they redeem the code. Send this or `coupon`.
        maxRedemptions:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 2147483647
          description: 'Redemptions across every code of the reward. Null: no limit.'
        redeemBy:
          type:
            - string
            - 'null'
          format: date-time
          description: No new redemptions after this time.
        oncePerCustomer:
          type: boolean
          description: >-
            A customer redeems the reward at most once, through any code.
            Defaults to true.
        promotionCodes:
          type: array
          items:
            $ref: '#/components/schemas/PromotionCodeInput'
          maxItems: 100
          description: >-
            The reward's catalog codes. Codes made with POST
            /v1/rewards/{rewardId}/promotion-codes are kept.
      required:
        - id
        - name
        - promotionCodes
      additionalProperties: false
    RewardMutation:
      type: object
      properties:
        item:
          $ref: '#/components/schemas/Reward'
        changed:
          type: boolean
      required:
        - item
        - changed
    RewardError:
      type: object
      properties:
        error:
          type: string
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
        issues:
          type: array
          items:
            type: object
            properties:
              path:
                type: string
              message:
                type: string
            required:
              - path
              - message
          description: >-
            With `invalid_request`: each field that failed validation and why.
            `path` is empty for the body itself, such as an unknown field.
      required:
        - error
    GrantItemInput:
      type: object
      properties:
        featureId:
          type: string
          minLength: 1
          maxLength: 100
          description: '`credits` for the shared credit pool, or a metered feature''s ID.'
        amount:
          type: integer
          exclusiveMinimum: 0
          maximum: 2147483647
          description: Credits, or the metered feature's units, above 0.
        expiresAfter:
          $ref: '#/components/schemas/GrantExpiresAfter'
      required:
        - featureId
        - amount
      additionalProperties: false
    PromotionCodeInput:
      type: object
      properties:
        code:
          type: string
          pattern: ^[A-Za-z0-9_-]{3,40}$
          description: >-
            What the customer types, such as `LAUNCH20`. Matched without case
            and stored in upper case. Unique among the environment's active
            codes.
        maxRedemptions:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 2147483647
          description: 'How many times the code can be used. Null: no limit.'
        expiresAt:
          type:
            - string
            - 'null'
          format: date-time
          description: No new redemptions after this time.
        firstTimeOnly:
          type: boolean
          description: Only for a customer who has never paid for a plan or credit pack.
      required:
        - code
      additionalProperties: false
    Reward:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        kind:
          type: string
          enum:
            - coupon
            - grant
        coupon:
          type:
            - object
            - 'null'
          properties:
            percentOff:
              type:
                - number
                - 'null'
            amountOffCents:
              type:
                - integer
                - 'null'
            currency:
              type:
                - string
                - 'null'
            duration:
              type: string
              enum:
                - once
                - repeating
                - forever
            durationMonths:
              type:
                - integer
                - 'null'
            planIds:
              type:
                - array
                - 'null'
              items:
                type: string
          required:
            - percentOff
            - amountOffCents
            - currency
            - duration
            - durationMonths
            - planIds
        grant:
          type:
            - object
            - 'null'
          properties:
            items:
              type: array
              items:
                $ref: '#/components/schemas/GrantItem'
          required:
            - items
        maxRedemptions:
          type:
            - integer
            - 'null'
        timesRedeemed:
          type: integer
        redeemBy:
          type:
            - string
            - 'null'
        oncePerCustomer:
          type: boolean
        archived:
          type: boolean
        stripeSyncStatus:
          type:
            - string
            - 'null'
          enum:
            - pending
            - synced
            - failed
            - null
          description: >-
            A coupon's Stripe sync. Null for a grant, which exists in Ledger
            alone.
        stripeSyncError:
          type:
            - string
            - 'null'
          description: >-
            Why the last Stripe sync didn't finish: `promotion_code_taken` (a
            code is active in Stripe already, such as a Dashboard code),
            `waiting_for_plan_products`, `too_many_products` (the coupon would
            cover more than the 100 Products a Stripe coupon takes: name fewer
            plans), or a Stripe failure. Null once synced.
        promotionCodes:
          type: array
          items:
            $ref: '#/components/schemas/PromotionCode'
      required:
        - id
        - name
        - kind
        - coupon
        - grant
        - maxRedemptions
        - timesRedeemed
        - redeemBy
        - oncePerCustomer
        - archived
        - stripeSyncStatus
        - stripeSyncError
        - promotionCodes
    InsufficientPermissionsError:
      type: object
      properties:
        error:
          type: string
          example: insufficient_permissions
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
        requiredScope:
          type: string
          example: customers:read
      required:
        - error
        - requiredScope
    PayloadTooLargeError:
      type: object
      properties:
        error:
          type: string
          example: payload_too_large
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
      required:
        - error
    RateLimitError:
      type: object
      properties:
        error:
          type: string
          example: rate_limited
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
      required:
        - error
    GrantExpiresAfter:
      type: object
      properties:
        days:
          type: integer
          minimum: 1
          maximum: 3650
        months:
          type: integer
          minimum: 1
          maximum: 120
      additionalProperties: false
      description: >-
        How long the granted units last from the redemption: `{ days }` or `{
        months }`. Left out: they never expire.
    GrantItem:
      type: object
      properties:
        featureId:
          type: string
        amount:
          type: integer
        expiresAfter:
          type:
            - object
            - 'null'
          properties:
            days:
              type: integer
            months:
              type: integer
      required:
        - featureId
        - amount
        - expiresAfter
    PromotionCode:
      type: object
      properties:
        code:
          type: string
        source:
          type: string
          enum:
            - catalog
            - api
        active:
          type: boolean
        customerId:
          type:
            - string
            - 'null'
        maxRedemptions:
          type:
            - integer
            - 'null'
        timesRedeemed:
          type: integer
        expiresAt:
          type:
            - string
            - 'null'
        firstTimeOnly:
          type: boolean
      required:
        - code
        - source
        - active
        - customerId
        - maxRedemptions
        - timesRedeemed
        - expiresAt
        - firstTimeOnly
  responses:
    InsufficientPermissions:
      description: >-
        The API key works but lacks the permission this endpoint needs, named in
        requiredScope. See the API keys guide.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/InsufficientPermissionsError'
    PayloadTooLarge:
      description: 'The request body is over the limit: 1 MiB, or 5 MiB for a webhook.'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/PayloadTooLargeError'
    RateLimited:
      description: >-
        Too many requests. Wait Retry-After seconds, then retry. See the rate
        limits guide.
      headers:
        Retry-After:
          description: Seconds until a request is allowed again
          schema:
            type: integer
        RateLimit-Limit:
          description: 'The burst: requests allowed at once'
          schema:
            type: integer
        RateLimit-Remaining:
          description: Requests left right now
          schema:
            type: integer
        RateLimit-Reset:
          description: Seconds until the full burst is back
          schema:
            type: integer
        RateLimit-Policy:
          description: >-
            The sustained rate per second and the burst, such as
            200;w=1;burst=1000
          schema:
            type: string
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/RateLimitError'
  securitySchemes:
    ApiKey:
      type: http
      scheme: bearer

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.