create_affiliate
Add new affiliates to your referral program by providing their email address. Each affiliate receives a unique referral code to start tracking customer referrals.
Instructions
Create a new affiliate in your Refgrow project. An affiliate receives a unique referral code and can start referring customers.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| Yes | Email address for the new affiliate | ||
| referral_code | No | Custom referral code (auto-generated if not provided) | |
| partner_slug | No | Partner slug for URL tracking |
Implementation Reference
- src/tools.ts:170-181 (handler)The async handler function for create_affiliate tool that builds the request body and makes a POST API call to /affiliates endpoint
async ({ email, referral_code, partner_slug }) => { try { const body: Record<string, unknown> = { email }; if (referral_code) body.referral_code = referral_code; if (partner_slug) body.partner_slug = partner_slug; const data = await apiRequest(config, "POST", "/affiliates", body); return textResult(data); } catch (err) { return errorResult(err); } } - src/tools.ts:157-169 (schema)Zod schema defining input validation for create_affiliate: email (required, email format), referral_code (optional), and partner_slug (optional)
{ email: z.string().email().describe("Email address for the new affiliate"), referral_code: z .string() .optional() .describe( "Custom referral code (auto-generated if not provided)" ), partner_slug: z .string() .optional() .describe("Partner slug for URL tracking"), }, - src/tools.ts:154-182 (registration)Complete tool registration with server.tool() including name 'create_affiliate', description, schema, and handler function
server.tool( "create_affiliate", "Create a new affiliate in your Refgrow project. An affiliate receives a unique referral code and can start referring customers.", { email: z.string().email().describe("Email address for the new affiliate"), referral_code: z .string() .optional() .describe( "Custom referral code (auto-generated if not provided)" ), partner_slug: z .string() .optional() .describe("Partner slug for URL tracking"), }, async ({ email, referral_code, partner_slug }) => { try { const body: Record<string, unknown> = { email }; if (referral_code) body.referral_code = referral_code; if (partner_slug) body.partner_slug = partner_slug; const data = await apiRequest(config, "POST", "/affiliates", body); return textResult(data); } catch (err) { return errorResult(err); } } ); - src/types.ts:37-41 (schema)TypeScript interface CreateAffiliateInput defining the type structure for create_affiliate input parameters
export interface CreateAffiliateInput { email: string; referral_code?: string; partner_slug?: string; } - src/tools.ts:17-61 (helper)apiRequest helper function that handles HTTP requests to the Refgrow API, including authentication and error handling
async function apiRequest<T = unknown>( config: RefgrowConfig, method: string, path: string, body?: Record<string, unknown>, query?: Record<string, string | number | boolean | undefined> ): Promise<ApiResponse<T>> { const url = new URL(`/api/v1${path}`, config.baseUrl); if (query) { for (const [key, val] of Object.entries(query)) { if (val !== undefined && val !== null) { url.searchParams.set(key, String(val)); } } } const headers: Record<string, string> = { Authorization: `Bearer ${config.apiKey}`, "Content-Type": "application/json", Accept: "application/json", }; const fetchOptions: RequestInit = { method, headers }; if (body && (method === "POST" || method === "PUT" || method === "PATCH")) { fetchOptions.body = JSON.stringify(body); } const response = await fetch(url.toString(), fetchOptions); // Handle 204 No Content (e.g. DELETE) if (response.status === 204) { return { success: true } as ApiResponse<T>; } const data = (await response.json()) as ApiResponse<T>; if (!response.ok) { throw new Error( data.error || `API request failed with status ${response.status}` ); } return data; }