# Flutter referral quickstart

A referral in Flutter takes two calls on `WarpLink`: `redeemInvitation(text)` reads the invitation an iPhone user pastes, and `claimReferral(installerId)` credits the install when the person signs up. On Android, the Play install referrer carries the invitation and the SDK matches it with no call of yours. This quickstart covers both platforms, then the webhook that tells your backend to grant the reward.

For the whole path, including the dashboard setup, read the [referrals guide](https://warplink.app/docs/guides/referrals).

## What you need

- An app registered in WarpLink: its App Store URL for iOS, and its package name and signing certificate SHA256 fingerprint for Android.
- An SDK key from **Keys**, then **SDK keys**. If your organization has several apps, create the key for this app.
- Flutter 3.38 or later and Dart 3.10 or later. The plugin runs on iOS and Android.
- A webhook endpoint, added under **Referral handoff**, then **Webhook**. See [Receive referral webhooks](https://warplink.app/docs/guides/webhooks).

## Install SDK 1.2.0

```bash
flutter pub add warplink_flutter
```

This adds `warplink_flutter: ^1.2.0` to `pubspec.yaml`. SDK 1.1.x cannot read an invitation. Call `WarpLink.configure` once from your root widget's `initState`, as shown in the [Flutter SDK setup](https://warplink.app/docs/sdks/flutter#configure-the-sdk).

## Mark a link as a referral link

Open a link in the dashboard and turn on **Referral link**, or send `is_referral` over REST:

```bash
curl -X POST https://api.warplink.app/v1/links \
  -H "Authorization: Bearer wl_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "app_id": "YOUR_APP_ID",
    "slug": "invite-ana",
    "destination_url": "https://example.com/invite",
    "is_referral": true
  }'
```

Store the returned link id with the user who shares it. The webhook names that link, and the id tells you whose invitation earned the credit. [Create referral links](https://warplink.app/docs/guides/referrals#create-referral-links) covers converting existing links in bulk.

## Read the invitation

**Android:** Google Play hands the install referrer to the SDK at first launch, and the invitation rides inside it. The match is exact (`android_referrer`) and needs no screen of yours.

**iPhone:** the install carries no data, so your first screen offers a field where the person pastes the invitation. Pass the text to `redeemInvitation`:

```dart
import 'package:warplink_flutter/warplink_flutter.dart';

Future<void> onInvitePasted(String pastedText) async {
  try {
    final link = await WarpLink.redeemInvitation(pastedText);
    if (link != null) showApplied(); // null: not an invitation, or no match
  } on WarpLinkServerException catch (error) {
    showMessage(error.message); // says how to open the link again
  } on WarpLinkNetworkException {
    showMessage('Check your connection and try again.');
  }
}
```

The package has no paste-button widget. The SDK never reads the clipboard, and it sees only the text you pass in. Keep a skip option: the paste step is an offer, not a gate, and onboarding continues when it returns `null` or throws.

After a `WarpLinkNetworkException` the native SDK keeps the invitation and tries again on a later launch. That later result arrives through `getAttributionResult()` and `claimReferral`, not through `onLink`.

## Claim the install

When the friend signs up, call `claimReferral` with an installer id: a dedicated random id for the new user, 1 to 128 characters. Do not use a primary key, an email address or a phone number.

```dart
Future<ReferralClaimResult?> claimForNewUser(String installerId) async {
  try {
    return await WarpLink.claimReferral(installerId);
  } on WarpLinkException catch (error) {
    // Unreachable server, refused key, or an empty or too long installer id.
    reportError(error);
  }
  return null;
}
```

`claimReferral` is safe to repeat. The first valid claim for an app and installer id wins, and the same call again returns the same claim.

### Handle each status

```dart
final claim = await claimForNewUser(user.warplinkId);
switch (claim?.status) {
  case ReferralClaimStatus.claimed:
    break; // credited inside the window. Your webhook grants the reward.
  case ReferralClaimStatus.flagged:
    break; // credited after the window: claim.flags has "outside_window"
  case ReferralClaimStatus.ignored:
    break; // already credited: see claim.duplicateOf
  case ReferralClaimStatus.none:
  case null:
    break; // no referral link brought this install, or the call failed
}
```

| `status` | What to do in the app |
|----------|----------------------|
| `ReferralClaimStatus.claimed` | Nothing more. The webhook is on its way |
| `ReferralClaimStatus.flagged` | Nothing more. Your backend decides from the `referral.flagged` event |
| `ReferralClaimStatus.ignored` | Nothing. A reward is never granted twice from our side |
| `ReferralClaimStatus.none` | Carry on. The install did not come from a referral link |

`none` is an ordinary answer, not an error. The result also carries `referralId`, `linkId`, `linkUrl`, `matchMethod`, `isExact`, `matchConfidence`, `flags`, `duplicateOf`, `updated` and `replayState`.

For rewards of value, claim from your backend with `POST /v1/referrals/claims` and let your own rules decide when a sign-up has earned one. See [Claim from the SDK or from your backend](https://warplink.app/docs/guides/referrals#claim-from-the-sdk-or-from-your-backend).

## Receive the webhook

Your endpoint receives `referral.claimed` within seconds of the claim. Verify the signature on the raw body, skip an event id you have seen, then grant the reward in your own system:

```js
if (event.type === 'referral.claimed') {
  await grantReward({
    inviterLinkId: event.data.link.id,
    newUserId: event.data.installer_id,
    exact: event.data.match.exact,
  });
}
```

[Receive referral webhooks](https://warplink.app/docs/guides/webhooks) has the signature code for Node.js and Python, and [Reward recipes](https://warplink.app/docs/guides/referrals/rewards) shows how to turn an event into a reward.

## Test it

Test on a real phone. On Android, install from a Google Play internal testing track. On iPhone, install from TestFlight or the App Store, then paste the invitation. [Test referrals](https://warplink.app/docs/guides/referrals/testing) lists the steps and the checks.

## Next

- [Referrals guide](https://warplink.app/docs/guides/referrals) for setup, credit windows and the report
- [Test referrals](https://warplink.app/docs/guides/referrals/testing) and [Reward recipes](https://warplink.app/docs/guides/referrals/rewards)
- [Flutter attribution](https://warplink.app/docs/sdks/flutter/attribution) for match types and evidence fields
- [iOS quickstart](https://warplink.app/docs/guides/referrals/ios), [Android quickstart](https://warplink.app/docs/guides/referrals/android), [React Native quickstart](https://warplink.app/docs/guides/referrals/react-native)
