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

# Update Affiliate

> Learn how to update an affiliate using the PUT /stores/{storeId}/affiliates/{affiliateId} endpoint.

## 📌 Endpoint

`PUT /stores/{storeId}/affiliates/{affiliateId}`

Updates an affiliate's data (name, email, code, image, status, metadata, and commission rules).

All fields are optional — only the sent fields are changed.

***

## 🔐 Authentication

* **Private route**.
* Requires the `Authorization` header with your API key and an active plan.

```json theme={null}
{
	"Authorization": "YOUR_API_KEY_HERE"
}
```

***

## 🧾 Route parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| `storeId` | string | Yes | Store ID |
| `affiliateId` | string | Yes | Affiliate ID |

***

## 📤 Body (JSON)

| Field | Type | Required | Rules |
| - | - | - | - |
| `name` | string | No | up to 56 characters |
| `email` | string | No | valid email, **unique per store** (cannot repeat another affiliate's email; case-insensitive comparison) |
| `code` | string | No | unique lowercase slug `^[a-z0-9]+(?:-[a-z0-9]+)*$` (3 to 50 characters) |
| `image` | string | No | valid `http`/`https` URL (up to 512 characters); sending `null` or `""` removes the image |
| `status` | string | No | `ativo` or `inativo` |
| `metadata` | object | No | extra data/notes about the affiliate (maximum of 64 keys) |
| `commissionRules` | array | No | non-empty array of commission rules: `[{ type, targetId, commission, position }]`. `type` ∈ `DEFAULT`, `PRODUCT`, `CATEGORY`, `COUPON`; `targetId` is the product/category ID or the coupon code (does not apply to `DEFAULT`); `commission` from 1 to 100; `position` is a unique integer `>= 0` per rule, defining priority — **lower `position` wins** when multiple rules match. The `DEFAULT` rule matches any sale and acts as the affiliate's **default percentage** (stays at the highest `position`) |

***

## ✅ Request example

```bash theme={null}
curl -X PUT "https://api.cabrapi.com.br/v1/stores/STORE_ID/affiliates/AFFILIATE_ID" \
	-H "Authorization: YOUR_API_KEY_HERE" \
	-H "Content-Type: application/json" \
	-d '{
		"name": "Bianca Rocha",
		"status": "ativo",
		"metadata": {
			"tag": "premium"
		},
		"commissionRules": [
			{
				"type": "PRODUCT",
				"targetId": "PRODUCT_ID",
				"commission": 15,
				"position": 0
			},
			{
				"type": "CATEGORY",
				"targetId": "CATEGORY_ID",
				"commission": 12,
				"position": 1
			},
			{
				"type": "COUPON",
				"targetId": "CUPOM10",
				"commission": 20,
				"position": 2
			},
			{
				"type": "DEFAULT",
				"commission": 10,
				"position": 3
			}
		]
	}'
```

***

## 📦 Success response (200)

```json theme={null}
{
	"code": "AFFILIATE_UPDATED",
	"affiliate": {
		"id": "AFFILIATE_ID",
		"name": "Bianca Rocha",
		"email": "bianca@email.com",
		"code": "bianca-rocha",
		"image": null,
		"status": "ativo",
		"pausedAt": null,
		"metadata": {
			"tag": "premium"
		},
		"commissionRules": [
			{
				"type": "PRODUCT",
				"targetId": "PRODUCT_ID",
				"commission": 15,
				"position": 0
			},
			{
				"type": "CATEGORY",
				"targetId": "CATEGORY_ID",
				"commission": 12,
				"position": 1
			},
			{
				"type": "COUPON",
				"targetId": "CUPOM10",
				"commission": 20,
				"position": 2
			},
			{
				"type": "DEFAULT",
				"commission": 10,
				"position": 3
			}
		],
		"storeId": "STORE_ID",
		"createdAt": "2026-09-08T19:30:00Z",
		"updatedAt": "2026-09-08T19:30:00Z"
	}
}
```

***

## ⚠️ Possible errors

* `400 INVALID_DATA` → invalid body
* `400 INVALID_DATA_NAME` → `name` outside 1 to 56 characters
* `400 INVALID_DATA_EMAIL` → invalid `email` or longer than 120 characters
* `400 INVALID_DATA_IMAGE` → invalid `image` (invalid `http`/`https` URL or longer than 512 characters)
* `400 INVALID_DATA_CODE` → `code` outside 3 to 50 characters or invalid slug format
* `400 INVALID_DATA_STATUS` → invalid `status` (must be `ativo` or `inativo`)
* `400 INVALID_DATA_METADATA` → invalid `metadata` (not an object or exceeds 64 keys)
* `400 INVALID_DATA_RULES` → invalid `commissionRules` (not an array)
* `400 INVALID_COMMISSION_RULES` → empty `commissionRules`
* `400 INVALID_COMMISSION_RULE_TYPE` → invalid `type`
* `400 INVALID_COMMISSION_RULE_POSITION` → invalid/duplicated `position`
* `400 INVALID_COMMISSION_VALUE` → `commission` outside 1 to 100
* `400 COMMISSION_RULE_DEFAULT_REQUIRED` → exactly one `DEFAULT` rule is required
* `400 INVALID_COMMISSION_RULE_TARGET` → `DEFAULT` with `targetId`, or `PRODUCT`/`CATEGORY`/`COUPON` without `targetId`
* `400 RELATION_NOT_FOUND` → product/category/coupon `targetId` does not exist in the store
* `401 API_KEY_NOT_PROVIDED` → `Authorization` header not provided
* `401 INVALID_API_KEY` → invalid API key
* `401 UNAUTHENTICATED` → unauthenticated user
* `403 USER_DISABLED` → user disabled
* `403 NO_ACTIVE_SUBSCRIPTION` → no active plan
* `404 STORE_NOT_FOUND` → store not found
* `404 AFFILIATE_NOT_FOUND` → affiliate not found
* `409 AFFILIATE_ALREADY_EXISTS` → `code` already used by another affiliate
* `409 AFFILIATE_EMAIL_ALREADY_EXISTS` → email already registered by another affiliate in the store
* `429 RATE_LIMIT_EXCEEDED` → per-minute rate limit exceeded
* `500 INTERNAL_SERVER_ERROR` → internal server error
* `503 SERVICE_UNAVAILABLE` → service unavailable


## OpenAPI

````yaml PUT /stores/{storeId}/affiliates/{affiliateId}
openapi: 3.1.0
info:
  title: caBRAPI
  version: 1.0.0
servers:
  - url: https://api.cabrapi.com.br/v1
security: []
paths:
  /stores/{storeId}/affiliates/{affiliateId}:
    put:
      tags:
        - Affiliates
      summary: Update affiliate
      operationId: updateAffiliate
      parameters:
        - $ref: '#/components/parameters/StoreId'
        - $ref: '#/components/parameters/AffiliateId'
      requestBody:
        required: true
        description: Affiliate fields to update.
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  minLength: 1
                  maxLength: 56
                email:
                  format: email
                  maxLength: 120
                  type: string
                code:
                  type: string
                  minLength: 3
                  maxLength: 50
                  pattern: ^[a-z0-9]+(?:-[a-z0-9]+)*$
                image:
                  type: string
                  maxLength: 512
                  nullable: true
                status:
                  enum:
                    - ativo
                    - inativo
                  type: string
                metadata:
                  type: object
                  maxProperties: 64
                  nullable: true
                commissionRules:
                  type: array
                  items:
                    type: object
                    required:
                      - type
                      - commission
                      - position
                    properties:
                      type:
                        enum:
                          - DEFAULT
                          - PRODUCT
                          - CATEGORY
                          - COUPON
                        type: string
                      targetId:
                        type: string
                      commission:
                        type: number
                        minimum: 1
                        maximum: 100
                      position:
                        type: integer
                        minimum: 0
              minProperties: 1
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  code:
                    example: AFFILIATE_UPDATED
                    type: string
                  affiliate:
                    $ref: '#/components/schemas/Affiliate'
                required:
                  - code
                  - affiliate
                type: object
          description: Update affiliate.
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Invalid request.
        '401':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Missing or invalid API key.
        '403':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Forbidden (inactive plan or insufficient permissions).
        '404':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Resource not found.
        '409':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Resource already exists.
        '429':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Rate limit exceeded.
        '500':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Internal server error.
        '503':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          description: Service unavailable.
      security:
        - ApiKey: []
components:
  parameters:
    StoreId:
      in: path
      name: storeId
      required: true
      schema:
        type: string
        minLength: 1
        maxLength: 50
    AffiliateId:
      in: path
      name: affiliateId
      required: true
      schema:
        type: string
        minLength: 1
        maxLength: 50
  schemas:
    Affiliate:
      description: Store affiliate (referral code, commission, and stats).
      properties:
        code:
          type: string
        commissionRules:
          items:
            properties:
              commission:
                type: number
              position:
                type: integer
              targetId:
                type: string
              type:
                enum:
                  - DEFAULT
                  - PRODUCT
                  - CATEGORY
                  - COUPON
                type: string
            required:
              - type
              - commission
              - position
            type: object
          nullable: true
          type: array
        createdAt:
          format: date-time
          type: string
        email:
          format: email
          nullable: true
          type: string
        id:
          format: uuid
          type: string
        image:
          nullable: true
          type: string
        metadata:
          nullable: true
          type: object
        name:
          type: string
        pausedAt:
          format: date-time
          nullable: true
          type: string
        status:
          enum:
            - ativo
            - inativo
          type: string
        storeId:
          format: uuid
          type: string
        updatedAt:
          format: date-time
          type: string
        statistics:
          description: Included when statistics=true.
          type: object
          properties:
            clicks:
              type: integer
            revenue:
              type: number
            sales:
              type: integer
        metrics:
          description: Included when statistics=true.
          type: object
          properties:
            commissions:
              type: object
              properties:
                totalCount:
                  type: integer
                totalValue:
                  type: number
                graph:
                  type: array
                  items:
                    type: object
                    properties:
                      date:
                        type: string
                      total:
                        type: number
                    required:
                      - date
                      - total
              required:
                - totalCount
                - totalValue
                - graph
            affiliateRevenue:
              type: object
              properties:
                totalCount:
                  type: integer
                totalValue:
                  type: number
                graph:
                  type: array
                  items:
                    type: object
                    properties:
                      date:
                        type: string
                      total:
                        type: number
                    required:
                      - date
                      - total
              required:
                - totalCount
                - totalValue
                - graph
          required:
            - commissions
            - affiliateRevenue
      required:
        - id
        - name
        - code
        - status
        - storeId
        - createdAt
        - updatedAt
      type: object
    Error:
      properties:
        code:
          example: STORE_NOT_FOUND
          type: string
      required:
        - code
      type: object
  securitySchemes:
    ApiKey:
      in: header
      name: Authorization
      type: apiKey
      x-default: YOUR_API_KEY_HERE

````

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