> ## 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.

# Redeem a promotion code

> Give a customer a coupon or a grant from a code they typed in your app.

Redeems one of your [reward](https://docs.integrateledger.com/billing/discounts.md) codes for a customer, such as one typed in your app's billing page. A [grant code](https://docs.integrateledger.com/billing/discounts.md#grant-codes) gives its credits and units at once: the redemption is `active`, with what it gave in `grants`. For a coupon code, on a current paid plan the coupon covers, Ledger puts it on the customer's subscription and it discounts from their next invoice (`appliesFrom: "next_invoice"`). Otherwise it waits for their next attach of a plan it covers (`next_attach`). A subscription carries one Ledger coupon at a time: a second one is `queued` and starts when the first ends.

Refusals, in the order Ledger checks: `404 promotion_code_not_found`, `409 promotion_code_expired`, `409 promotion_code_not_for_customer`, `409 promotion_code_first_time_only` (the customer has paid for a plan or pack), `409 promotion_code_already_redeemed`, `409 promotion_code_exhausted`. Send an `Idempotency-Key` header to retry safely: a retry returns the first redemption. Needs the **Billing** permission.


## OpenAPI

````yaml openapi.json POST /v1/customers/redemptions
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/customers/redemptions:
    post:
      tags:
        - Customers
      summary: Redeem a promotion code for a customer
      operationId: redeemPromotionCode
      parameters:
        - schema:
            type: string
            minLength: 1
            maxLength: 255
            description: Your ID for the customer.
          required: true
          description: Your ID for the customer.
          name: customerId
          in: query
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RedeemInput'
      responses:
        '200':
          description: >-
            Redeemed. A grant code's credits and units are the customer's at
            once (`grants`). On a current plan a coupon covers, Ledger puts it
            on the customer's subscription and it discounts from the next
            invoice; otherwise it waits for their next attach of a covered plan.
            A repeat with the same Idempotency-Key returns the first redemption.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RedeemResult'
        '400':
          description: Invalid request
          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'
        '404':
          description: >-
            No such customer (`customer_not_found`) or active code
            (`promotion_code_not_found`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '409':
          description: >-
            The code expired (`promotion_code_expired`), is another customer's
            (`promotion_code_not_for_customer`), is for first-time customers
            (`promotion_code_first_time_only`), was redeemed by this customer
            (`promotion_code_already_redeemed`), or is used up
            (`promotion_code_exhausted`); or, for a referral code, the customer
            was referred in the program already (`already_referred`), has paid
            for a plan or pack (`referee_not_new`), or the referrer has the
            program's most rewards (`referrer_limit_reached`, at signup)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '422':
          description: >-
            A referrer redeeming their own referral code, or one on the same
            Stripe customer (`self_referral`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RewardError'
        '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.redeem_promotion_code(customer_id="user_123",
            code="LAUNCH20")
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/customers/redemptions?customerId=user_123'
            \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"code":"LAUNCH20"}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.redeemPromotionCode({ customerId: "user_123", body: {
            code: "LAUNCH20" } });
components:
  schemas:
    RedeemInput:
      type: object
      properties:
        code:
          type: string
          minLength: 1
          maxLength: 40
          description: The code the customer typed, any case.
      required:
        - code
      additionalProperties: false
    RedeemResult:
      type: object
      properties:
        redemptionId:
          type: integer
        rewardId:
          type: string
        kind:
          type: string
          enum:
            - coupon
            - grant
        code:
          type:
            - string
            - 'null'
        status:
          type: string
          enum:
            - pending
            - queued
            - active
            - ended
            - void
          description: >-
            pending: waiting for a bill (the customer's next attach of a covered
            plan, or Ledger putting it on their current one). queued: their bill
            already has a Ledger discount; it starts when that one ends. active:
            on the bill, or a grant's credits and units granted. ended: used up,
            or the bill ended. void: never applied, such as an abandoned
            Checkout; its use was given back.
        appliesFrom:
          type:
            - string
            - 'null'
          enum:
            - next_invoice
            - next_attach
            - null
          description: >-
            While pending or queued: the customer's next invoice on their
            current plan, or their next attach of a plan the coupon covers.
        subscriptionId:
          type:
            - integer
            - 'null'
        discount:
          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'
          required:
            - percentOff
            - amountOffCents
            - currency
            - duration
            - durationMonths
          description: A coupon's terms. Null for a grant.
        grants:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/RedemptionGrant'
          description: >-
            What a grant gave the customer, each with when it expires. Null for
            a coupon.
        endsAt:
          type:
            - string
            - 'null'
        endedReason:
          type:
            - string
            - 'null'
          enum:
            - duration
            - subscription_ended
            - removed
            - null
        createdAt:
          type: string
        referral:
          $ref: '#/components/schemas/Referral'
      required:
        - referral
    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
    RedemptionGrant:
      type: object
      properties:
        featureId:
          type: string
          description: '`credits` for the credit pool, or the metered feature''s ID.'
        amount:
          type: integer
        expiresAt:
          type:
            - string
            - 'null'
      required:
        - featureId
        - amount
        - expiresAt
    Referral:
      type:
        - object
        - 'null'
      properties:
        referralId:
          type: integer
        programId:
          type: string
        referrerId:
          type: string
        refereeId:
          type: string
        status:
          type: string
          enum:
            - pending
            - rewarded
            - refused
          description: >-
            pending: waiting for the trigger (the referee's first paid invoice).
            rewarded: the referrer got their reward. refused: see
            `refusedReason`; a referee keeps their own reward.
        refusedReason:
          type:
            - string
            - 'null'
          enum:
            - same_payment_method
            - referrer_limit_reached
            - null
          description: >-
            `same_payment_method`: the referee paid with a card the referrer has
            saved. `referrer_limit_reached`: the referrer had the program's most
            rewards already.
        rewardedAt:
          type:
            - string
            - 'null'
        createdAt:
          type: string
      required:
        - referralId
        - programId
        - referrerId
        - refereeId
        - status
        - refusedReason
        - rewardedAt
        - createdAt
      description: >-
        The referral, when the code was a referrer's: `rewarded` at once for a
        `signup` program, `pending` until the referee's first payment for
        `first_payment`. Null for a promotion code.
    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
  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.