BIO.RE
Referral

Get Referral Link

Get-or-create the calling user's personal referral link. First call generates a code from their username (or random UUID slice on collision); subsequent calls return the existing one.

GET /api/v1/referral/link โ€” ๐Ÿ”‘ Bearer ยท Kill-switched

Returns the calling user's personal referral link. Get-or-create: first read generates a ReferralLink row (code derived from User.username, falling back to an 8-char UUID slice on collision); every subsequent read returns the existing row.

Cross-table uniqueness. The ReferralLink.code must NOT collide with any User.referralCode (a separate column on the User table โ€” also a referral identifier). The server retries up to 3 times with random UUID slices on collision; if all 3 retry, the request fails with 400 code_collision.

Username drives the default code. If the user's username is alice123, their referral link will be bio.re/ref/alice123 โ€” username and referral code are linked at first creation, but subsequent username changes do NOT update the existing code.

Request

No body, no params.

HeaderRequiredNotes
Authorization: Bearer <accessToken>โœ“JWT from POST /auth/login

Response

200 OK โ€” ApiResponseOf<ReferralLinkCodeResponseDto>

{
  "success": true,
  "data": {
    "code": "alice123",
    "link": "bio.re/ref/alice123"
  }
}
FieldTypeNotes
codestringThe referral identifier โ€” pass to POST /referral/click/:code for tracking
linkstringThe full shareable URL โ€” bio.re/ref/<code>

Errors

HTTPcode / i18nKeyReason
400referral.link.code_collisionFailed to generate a unique code after 3 retries (rare โ€” would require username-vs-other-user collision plus 2 UUID slice collisions in a row)
401(guard)Missing / invalid bearer token
503features.referral_disabledAdmin kill switch REFERRAL is active

Side effects

  1. referralLink.findUnique({ where: { userId } }). Existing row โ†’ return { code, link } directly.
  2. No row โ€” load User.username for the candidate code; fallback to randomUUID().slice(0, 8) if null.
  3. Cross-table collision check (up to 3 attempts):
    • user.findUnique({ where: { referralCode: code } }) โ€” if hit, regenerate with randomUUID().slice(0, 8).
    • 3rd failure โ†’ throw code_collision.
  4. referralLink.create({ id, userId, code }).
  5. Return { code, link: 'bio.re/ref/<code>' }.

Code samples

curl https://api.bio.re/api/v1/referral/link \
  -H "Authorization: Bearer $ACCESS_TOKEN"
type ReferralLink = {
  code: string;
  link: string;
};

async function getReferralLink(accessToken: string): Promise<ReferralLink> {
  const res = await fetch('https://api.bio.re/api/v1/referral/link', {
    headers: { Authorization: `Bearer ${accessToken}` },
  });
  const json = await res.json();
  if (!res.ok || !json.success) {
    throw Object.assign(new Error(json?.error?.message ?? 'Referral link fetch failed'), {
      code: json?.error?.code,
    });
  }
  return json.data;
}
import { useQuery } from '@tanstack/react-query';

export const referralKeys = {
  link: () => ['referral', 'link'] as const,
};

export function useReferralLink() {
  return useQuery({
    queryKey: referralKeys.link(),
    queryFn: async () => {
      const res = await fetch('/api/v1/referral/link');
      const json = await res.json();
      if (!res.ok || !json.success) {
        throw Object.assign(new Error(json?.error?.message ?? 'Referral link fetch failed'), {
          code: json?.error?.code,
          i18nKey: json?.error?.i18nKey,
        });
      }
      return json.data as ReferralLink;
    },
    staleTime: Infinity, // code never changes after first creation
    gcTime: 60 * 60_000,
  });
}

Try it

GET
/api/v1/referral/link
AuthorizationBearer <token>

In: header

Response Body

application/json

application/json

curl -X GET "https://loading/api/v1/referral/link"
{
  "success": true,
  "data": {
    "code": "alice123",
    "link": "bio.re/ref/alice123"
  }
}
{
  "success": false,
  "error": {
    "code": "AUTH_UNAUTHORIZED",
    "message": "Invalid credentials",
    "i18nKey": "auth.login.invalid_credentials",
    "i18nVars": {
      "field": "email"
    },
    "details": [
      {
        "message": "email must be an email"
      }
    ],
    "correlationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
  }
}

Source

SourcePathLines
Controllerapps/api-core/src/modules/referral/referral.controller.ts42โ€“47 (getLink)
DTO (response)apps/api-core/src/modules/referral/dto/referral-response.dto.ts298โ€“304 (ReferralLinkCodeResponseDto)
Serviceapps/api-core/src/modules/referral/referral.service.ts23โ€“47 (getOrCreateLink)
Kill switchapps/api-core/src/common/guards/kill-switch.guard.tsRequireKillSwitch('REFERRAL') (class-level)
Prisma modelspackages/prisma/prisma/schema.prismaReferralLink (unique userId, unique code), User.referralCode (cross-table uniqueness check)

On this page