Skip to content

Commit ae45964

Browse files
authored
Merge pull request #700 from Merit-Systems/br/add-echo-referral-handler
add docs
2 parents 5af2387 + 83a1e91 commit ae45964

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

packages/app/control/docs/money/referrals.mdx

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,150 @@ User referrals allow you to incentivize users to market your application. Set a
1818

1919
With a 5% referral rate, if a referred user generates $100 in profit for your app, the referrer earns $5.
2020

21+
## Implementation Guide
22+
23+
To implement referrals in your application, you'll need to handle two key flows: generating referral links and processing incoming referrals.
24+
25+
### 1. Generating Referral Links
26+
27+
Allow users to generate and share their referral links using the GET endpoint:
28+
29+
```typescript
30+
// Fetch user's referral code
31+
const response = await fetch('/api/v1/user/referral?echoAppId=your_app_id', {
32+
headers: {
33+
Authorization: `Bearer ${userToken}`,
34+
},
35+
});
36+
37+
const data = await response.json();
38+
// Returns:
39+
// {
40+
// success: true,
41+
// code: "ABC123XYZ",
42+
// referralLinkUrl: "https://yourapp.com?referral_code=ABC123XYZ",
43+
// expiresAt: "2025-11-25T00:00:00.000Z"
44+
// }
45+
```
46+
47+
Display the `referralLinkUrl` in your UI for users to copy and share.
48+
49+
### 2. Processing Incoming Referrals
50+
51+
When users land on your app with a `referral_code` query parameter, automatically register the referral relationship.
52+
53+
Create a client component similar to this:
54+
55+
```tsx
56+
'use client';
57+
58+
import { useEffect, useState } from 'react';
59+
import { useSearchParams } from 'next/navigation';
60+
61+
interface Props {
62+
appId: string;
63+
}
64+
65+
export const ReferralHandler: React.FC<Props> = ({ appId }) => {
66+
const searchParams = useSearchParams();
67+
const referralCode = searchParams.get('referral_code');
68+
const [processed, setProcessed] = useState(false);
69+
70+
useEffect(() => {
71+
if (!referralCode || processed) return;
72+
73+
const processReferralCode = async () => {
74+
await fetch('/api/v1/user/referral', {
75+
method: 'POST',
76+
headers: {
77+
'Content-Type': 'application/json',
78+
Authorization: `Bearer ${userToken}`,
79+
},
80+
body: JSON.stringify({
81+
echoAppId: appId,
82+
code: referralCode,
83+
}),
84+
}).catch(() => {
85+
// Silently fail - code may be invalid, expired, or user may already have a referrer
86+
});
87+
88+
setProcessed(true);
89+
};
90+
91+
void processReferralCode();
92+
}, [referralCode, appId, processed]);
93+
94+
return null;
95+
};
96+
```
97+
98+
### 3. Placement
99+
100+
Mount the `ReferralHandler` component on key entry points to your app:
101+
- Sign up pages
102+
- Landing pages
103+
- Dashboard/home pages after authentication
104+
105+
```tsx
106+
// In your app's main page or layout
107+
<ReferralHandler appId={yourAppId} />
108+
```
109+
110+
### API Reference
111+
112+
#### GET /api/v1/user/referral
113+
114+
Retrieves or creates a referral code for the authenticated user.
115+
116+
**Query Parameters:**
117+
- `echoAppId` (required): The ID of your Echo app
118+
119+
**Response:**
120+
```json
121+
{
122+
"success": true,
123+
"message": "Referral code retrieved successfully",
124+
"code": "ABC123XYZ",
125+
"referralLinkUrl": "https://yourapp.com?referral_code=ABC123XYZ",
126+
"expiresAt": "2025-11-25T00:00:00.000Z"
127+
}
128+
```
129+
130+
#### POST /api/v1/user/referral
131+
132+
Applies a referral code to the authenticated user.
133+
134+
**Request Body:**
135+
```json
136+
{
137+
"echoAppId": "your_app_id",
138+
"code": "ABC123XYZ"
139+
}
140+
```
141+
142+
**Success Response:**
143+
```json
144+
{
145+
"success": true,
146+
"message": "Referral code applied successfully"
147+
}
148+
```
149+
150+
**Error Response (400):**
151+
```json
152+
{
153+
"success": false,
154+
"message": "Referral code could not be applied. It may be invalid, expired, or you may already have a referrer for this app."
155+
}
156+
```
157+
158+
### Important Notes
159+
160+
- Referral codes are unique per user per app
161+
- A user can only have one referrer per app (first come, first served)
162+
- Invalid or expired codes should fail silently on the client to avoid UX disruption
163+
- Referral earnings are calculated and paid out automatically by Echo
164+
21165
## Beta Status
22166

23167
This feature is in early beta and may not work as expected. Reach out in [Discord](https://discord.gg/merit) if you're interested in setting up referrals for your application.

packages/app/control/src/app/(app)/app/[id]/(overview)/_components/referral-handler.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* Referral Handler Component
3+
*
4+
* Processes referral codes from URL query parameters (?referral_code=ABC123) and automatically
5+
* registers the referral relationship. Errors are silently caught since codes may be invalid,
6+
* expired, or the user may already have a referrer.
7+
*
8+
* See /docs/money/referrals.mdx for implementation guide.
9+
*/
110
'use client';
211

312
import { useEffect, useState } from 'react';

packages/app/control/src/app/api/v1/user/referral/route.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* Referral API Routes
3+
*
4+
* GET /api/v1/user/referral - Retrieves or creates a user's referral code
5+
* POST /api/v1/user/referral - Applies a referral code to establish referrer relationship
6+
*
7+
* See /docs/money/referrals.mdx for full documentation and implementation guide.
8+
*/
19
import { NextResponse } from 'next/server';
210
import { z } from 'zod';
311
import { appIdSchema } from '@/services/db/apps/lib/schemas';

0 commit comments

Comments
 (0)