> ## 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 referral program

> Name a reward for the referrer, and one for the new customer.

Creates or updates one [referral program](https://docs.integrateledger.com/billing/referrals.md), in the catalog's shape. Its rewards must be active rewards of the environment (`unknown_reward`). A change applies to referrals from then on; referrals already rewarded keep what they got. The catalog's `referralPrograms` writes the same programs. Needs the **Catalog** permission at Write.


## OpenAPI

````yaml openapi.json POST /v1/referral-programs
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/referral-programs:
    post:
      tags:
        - Rewards
      summary: Create or update a referral program
      operationId: writeReferralProgram
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ReferralProgramInput'
      responses:
        '200':
          description: The program as written.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReferralProgramMutation'
        '400':
          description: >-
            Invalid request, or a program naming an unknown reward
            (`unknown_reward`) or plan (`invalid_reward`)
          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'
        '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_referral_program({"id": "friends", "name": "Refer a
            friend", "referrerRewardId": "welcome", "refereeRewardId":
            "launch20", "trigger": "first_payment"})
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/referral-programs' \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"id":"friends","name":"Refer a friend","referrerRewardId":"welcome","refereeRewardId":"launch20","trigger":"first_payment","maxRewardsPerReferrer":10}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.writeReferralProgram({ body: { id: "friends", name:
            "Refer a friend", referrerRewardId: "welcome", refereeRewardId:
            "launch20", trigger: "first_payment", maxRewardsPerReferrer: 10 }
            });
components:
  schemas:
    ReferralProgramInput:
      type: object
      properties:
        id:
          type: string
          minLength: 1
          maxLength: 100
          pattern: ^[a-zA-Z0-9_-]+$
          description: >-
            Your ID for the program: letters, numbers, `_` or `-`. It can't be
            changed.
        name:
          type: string
          minLength: 1
          maxLength: 120
          description: Shown in the dashboard.
        referrerRewardId:
          type: string
          minLength: 1
          maxLength: 100
          description: 'The reward the referrer gets: a coupon or a grant.'
        refereeRewardId:
          type:
            - string
            - 'null'
          minLength: 1
          maxLength: 100
          description: >-
            The reward the new customer gets. Null or left out: only the
            referrer is rewarded.
        trigger:
          type: string
          enum:
            - signup
            - first_payment
          description: >-
            `signup`: both rewards when the referee redeems the code.
            `first_payment`: the referrer's reward (and a referee grant) when
            the referee first pays for a plan.
        planIds:
          type:
            - array
            - 'null'
          items:
            type: string
            minLength: 1
            maxLength: 100
          minItems: 1
          description: >-
            `first_payment` only: the paid plans whose first payment counts,
            every version of each. Null or left out: any paid plan.
        maxRewardsPerReferrer:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 2147483647
          description: 'The most referrer rewards one referrer gets. Null: no limit.'
      required:
        - id
        - name
        - referrerRewardId
        - trigger
      additionalProperties: false
    ReferralProgramMutation:
      type: object
      properties:
        item:
          $ref: '#/components/schemas/ReferralProgram'
        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
    ReferralProgram:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        referrerRewardId:
          type: string
        refereeRewardId:
          type:
            - string
            - 'null'
        trigger:
          type: string
          enum:
            - signup
            - first_payment
        planIds:
          type:
            - array
            - 'null'
          items:
            type: string
        maxRewardsPerReferrer:
          type:
            - integer
            - 'null'
        archived:
          type: boolean
      required:
        - id
        - name
        - referrerRewardId
        - refereeRewardId
        - trigger
        - planIds
        - maxRewardsPerReferrer
        - archived
    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.