<!-- ZenPays documentation · https://docs.zenpayz.com/docs/rest-api/endpoints/vendors/create-vendor-referral -->

# Create Vendor Referral

Create a new referral record linking a merchant to a vendor for commission tracking.

<EndpointHeader verb="POST" path="/merchant/api/v1/vendors/:id/referrals" />

## Request

### Headers

<ParamTable
  label="Header"
  rows={[
    { name: "Authorization", required: true, desc: "`Bearer {api_key}`" },
    { name: "X-Signature", required: true, desc: "HMAC-SHA256 signature" },
    { name: "X-Timestamp", required: true, desc: "ISO 8601 timestamp" },
    { name: "X-Secret-Salt", required: true, desc: "Secret salt for HMAC validation" },
    { name: "Content-Type", required: true, desc: "`application/json`" },
  ]}
/>

### Path Parameters

<ParamTable
  rows={[
    { name: "id", type: "string", required: true, desc: "Vendor ID (e.g., `vnd_a1b2c3d4e5f6g7h8`)" },
  ]}
/>

### Body Parameters

<ParamTable
  rows={[
    { name: "merchantId", type: "string", required: true, desc: "ID of the referred merchant" },
    { name: "referralCode", type: "string", desc: "Custom referral tracking code" },
    { name: "referralSource", type: "string", desc: "Source of the referral (e.g., `website`, `partner_portal`, `event`)" },
    { name: "commissionRate", type: "number", desc: "Override commission rate for this referral (0-100)" },
    { name: "environment", type: "string", desc: "Environment identifier" },
    { name: "notes", type: "string", desc: "Additional notes about the referral" },
  ]}
/>

## Response

### Success (201 Created)

```json
{
  "success": true,
  "data": {
    "referralId": "ref_j0k1l2m3n4o5p6q7",
    "vendorId": "vnd_a1b2c3d4e5f6g7h8",
    "merchantId": "mer_m3n4o5p6q7r8s9t0",
    "referralCode": "ACME-2024-003",
    "referralSource": "partner_portal",
    "status": "active",
    "commissionRate": 15.0,
    "totalTransactions": 0,
    "totalRevenue": 0,
    "totalCommission": 0,
    "environment": "production",
    "notes": "Enterprise client referred at FinTech Summit 2024",
    "createdAt": "2024-03-21T11:00:00.000Z",
    "updatedAt": "2024-03-21T11:00:00.000Z"
  },
  "message": "Vendor referral created successfully",
  "error": null,
  "meta": {
    "request_id": "req_xxxxx",
    "timestamp": "2024-03-21T11:00:00.000Z",
    "processing_time_ms": 130,
    "api_version": "v1",
    "endpoint": "POST /vendors/:id/referrals",
    "user_type": "merchant"
  }
}
```

### Error Responses

<ErrorTable
  rows={[
    { code: "VALIDATION_ERROR", status: "400", message: "Invalid request parameters" },
    { code: "UNAUTHORIZED", status: "401", message: "Invalid API key" },
    { code: "VENDOR_NOT_FOUND", status: "404", message: "Vendor with specified ID does not exist" },
    { code: "MERCHANT_NOT_FOUND", status: "404", message: "Merchant with specified ID does not exist" },
    { code: "DUPLICATE_REFERRAL", status: "409", message: "Referral for this merchant already exists under this vendor" },
  ]}
/>

<CodeRail>

## Examples

<Tabs groupId="language">
  <TabItem value="curl" label="cURL" default>

```bash
curl -X POST https://api.zenpayz.com/merchant/api/v1/vendors/vnd_a1b2c3d4e5f6g7h8/referrals \
  -H "Authorization: Bearer zp_test_xxxxx" \
  -H "X-Timestamp: 2024-03-21T11:00:00.000Z" \
  -H "X-Signature: a1b2c3d4e5f6..." \
  -H "X-Secret-Salt: your_secret_salt" \
  -H "Content-Type: application/json" \
  -d '{
    "merchantId": "mer_m3n4o5p6q7r8s9t0",
    "referralCode": "ACME-2024-003",
    "referralSource": "partner_portal",
    "commissionRate": 15.0,
    "environment": "production",
    "notes": "Enterprise client referred at FinTech Summit 2024"
  }'
```

  </TabItem>
  <TabItem value="javascript" label="JavaScript">

```javascript
const response = await fetch('https://api.zenpayz.com/merchant/api/v1/vendors/vnd_a1b2c3d4e5f6g7h8/referrals', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'X-Timestamp': timestamp,
    'X-Signature': signature,
    'X-Secret-Salt': secretSalt,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    merchantId: 'mer_m3n4o5p6q7r8s9t0',
    referralCode: 'ACME-2024-003',
    referralSource: 'partner_portal',
    commissionRate: 15.0,
    environment: 'production',
    notes: 'Enterprise client referred at FinTech Summit 2024',
  }),
});

const result = await response.json();
console.log(result.data.referralId); // ref_j0k1l2m3n4o5p6q7
```

  </TabItem>
  <TabItem value="sdk" label="SDK">

```javascript
const referral = await zenpays.vendors.createReferral('vnd_a1b2c3d4e5f6g7h8', {
  merchantId: 'mer_m3n4o5p6q7r8s9t0',
  referralCode: 'ACME-2024-003',
  referralSource: 'partner_portal',
  commissionRate: 15.0,
  environment: 'production',
  notes: 'Enterprise client referred at FinTech Summit 2024',
});

console.log(referral.referralId); // ref_j0k1l2m3n4o5p6q7
```

  </TabItem>
</Tabs>

</CodeRail>

## Related Endpoints

- [Vendor Referrals](/docs/rest-api/endpoints/vendors/vendor-referrals)
- [Vendor Commissions](/docs/rest-api/endpoints/vendors/vendor-commissions)
- [Get Vendor](/docs/rest-api/endpoints/vendors/get-vendor)
