> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stash.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# Account Linking

> Learn how to connect a player's webshop session with their game client through account linking. Understand deep link setup, integration steps for different engines, and Unity-specific implementation using the Stash SDK.

Account linking connects a player's webshop session with their game client.

The webshop launches the game through a deep link (on mobile) or a QR code (on desktop). The deep link carries a session code. The game client attaches the player's authentication data to this code and sends both to the Stash API to complete login.

## Deeplink setup

Set up the login deep link on the **App Login** page in Stash Studio. Use a URL scheme that clearly represents your game, and register it in your game client on each platform. For example, the demo app uses `howlingwoods://`.

**1. Open App Login**

Open your game in Stash Studio. In the main menu, click **Webshop**, then **App Login**.

**2. Turn on App Login**

Turn on **Enable App Login** and enter a **Button label**. Players see a "Sign in with" button with that label.

**3. Enter the login deep link**

In **Login deep link**, enter your app's deep link with `%s` where the login code goes, for example `yourscheme://stash/login?code=%s`. Use `%s` exactly once. This is not the Account Linking URL scheme.

**4. Set a minimum app version (optional)**

In **Minimum app version (optional)**, enter the oldest app version that can sign in, as numbers separated by dots, for example `2.37.0`. Stash declines a login from an older version with `CLIENT_OUTDATED` and the minimum version, so your game can ask the player to update. Leave it empty for no minimum. See [When Stash declines an outdated app](#when-stash-declines-an-outdated-app).

To require a different version on one platform, also fill in **Minimum iOS app version (optional)** or **Minimum Android app version (optional)**. A platform's own minimum applies to players on that platform instead of **Minimum app version**. Leave a platform field empty to use **Minimum app version** there.

Stash picks the minimum for the platform your game server sends with the login. When no platform is sent, **Minimum app version** applies.

**5. Save**

Click **Save**. Changes are live on your web shop right away. On desktop, players scan a QR code that opens the **QR landing page** shown on the same page, which then opens your app.

## Integrate account linking

The example below uses Unity, but you can follow the same flow in other engines such as Unreal Engine, Godot, or custom web/native clients.

**1. Obtain the Player's Authentication Token**

Use the platform's authentication system (Google, Apple, or other providers) to retrieve a valid token or credential for the player.

**2. Extract the Code**

Parse the deep link or callback URL to extract the `code` parameter.

**3. Call the Stash Linking Endpoint**

Send the code and the player's authentication data to your game server. From there, call [`ApproveCustomLogin`](https://docs.stash.gg/api/ingress/webshop-account-linking/approvecustomlogin) (`POST /sdk/custom_login/approve`) to complete the link.

> [!NOTE]
>
> `ApproveCustomLogin` is a server-side endpoint. Call it from your game server, not from the game client directly.

Refer to your engine's documentation for handling deep links. The game client extracts the code and forwards it to your game server. The server completes the link by calling [`ApproveCustomLogin`](https://docs.stash.gg/api/ingress/webshop-account-linking/approvecustomlogin).

### Login code rules

* **Lifetime.** A code is valid for 10 minutes from when the webshop creates it. Approve it inside that window.
* **Single use.** A code can be approved once.
* **Format.** The code is URL-safe base64 and can end in `=`. Pass it to `ApproveCustomLogin` exactly as it appears after `code=` in the link.

`ApproveCustomLogin` answers:

| HTTP status | Message | Meaning |
| :- | :- | :- |
| `200` | | Approved, when the body has no `decline`. The webshop that showed the code can now sign the player in. |
| `200` | body has `decline` | Stash declined the login because the player's app is too old. The code isn't approved. See [When Stash declines an outdated app](#when-stash-declines-an-outdated-app). |
| `400` | `already approved` | The code was approved before. Have the player start sign-in again from the webshop. |
| `400` | `already declined` | The code was declined before, by your server or by Stash. Have the player start sign-in again from the webshop. A retry that sends `user.clientVersion` for a code declined with `CLIENT_OUTDATED` gets `200` with `decline` instead. |
| `403` | `invalid code` | The code has expired, is malformed, or was issued for another shop. |
| `401` | `invalid auth` | The request signature or API key was rejected. See [API Keys](https://docs.stash.gg/guides/get-started/stash-api-keys/overview.md). |

## After approval

After `ApproveCustomLogin` returns `200`, the webshop page that showed the code finishes the sign-in. It keeps checking for the approval while the player has it open, so the game only has to get the player back to that page.

**1. Don't open the webshop**

Don't open the webshop URL from the game after approval. That loads a new page, often in a new tab, and a new page can't finish the sign-in: only the page that created the code completes it.

**2. Send the player back to the browser**

Show a message in the game asking the player to switch back to their browser. This works on every platform and browser. Where your platform can bring the browser back to the front without opening a page, the game can do that as well.

**3. The webshop signs the player in**

Back on the page, the player is signed in within a few seconds, as long as the code is still inside its 10-minute lifetime.

> [!NOTE]
>
> On desktop, the player scans the QR code with their phone and the page that showed the code stays open on the computer. The game approves the code as usual and opens nothing; the page on the computer finishes the sign-in.

## Decline a login

When your game won't sign the player in to the webshop, call [`DeclineCustomLogin`](https://docs.stash.gg/api/ingress/webshop-account-linking/declinecustomlogin) (`POST /sdk/custom_login/decline`) from your game server instead of approving the code. Send the login code and a `reason`. A declined code can't be approved, and repeating the decline with the same `reason` succeeds again.

| `reason` | Send it when |
| :- | :- |
| `NOT_ELIGIBLE` | The player hasn't unlocked the webshop in your game yet. |
| `NOT_IN_ROLLOUT` | The player isn't in your webshop rollout: your feature flag is off for them, whether for this one player or for every player. |
| `CLIENT_OUTDATED` | The player's app is too old for the webshop. `DeclineCustomLogin` carries no minimum version. To have Stash check the version and return the minimum for you, see [When Stash declines an outdated app](#when-stash-declines-an-outdated-app). |
| `OTHER` | None of the above applies. |

For every reason, the webshop doesn't sign the player in. It shows the player a short message for the reason you sent, in the page's language:

| `reason` | What the message says |
| :- | :- |
| `NOT_ELIGIBLE` | The webshop isn't available for the player's account yet. |
| `NOT_IN_ROLLOUT` | The webshop isn't available yet. Try again soon. |
| `CLIENT_OUTDATED` | Update your app to the latest version to use the webshop. When your shop has a minimum app version, the message names that version. |
| `OTHER` | The webshop couldn't sign the player in. Try again later. |

The message gives no detail beyond that. Tell the player in your game why the webshop isn't available to them.

Each decline sends a [`LOGIN_FAILED`](https://docs.stash.gg/guides/get-started/stash-webhooks/webhook-list) webhook with `declinedBy` set to `"PARTNER"`.

> [!NOTE]
>
> Turning the webshop off for every player at once is a separate setting, not a `reason` your server sends: put your webshop in [maintenance mode](https://docs.stash.gg/guides/stash-webshop/how-tos/webshop-maintenance-mode.md). While it's on, players see a maintenance page and can't sign in or buy.

### When Stash declines an outdated app

Stash declines a login itself, with `CLIENT_OUTDATED`, when all of these hold:

* Your shop has a [minimum app version](#set-a-minimum-app-version-optional).
* Your server sends the player's app version as `user.clientVersion`.
* That version is below the minimum that applies to the `user.platform` your server sends. With no platform, **Minimum app version** applies.

Without `user.clientVersion`, Stash doesn't check the version.

The declined call still returns `200`, with a `decline` object in place of its usual result:

| Call | What changes |
| :- | :- |
| [`ApproveCustomLogin`](https://docs.stash.gg/api/ingress/webshop-account-linking/approvecustomlogin) | The code isn't approved, and can't be approved later. |
| [Generate authenticated URL](https://docs.stash.gg/api/ingress/server-urls/generateauthenticatedurl.md) | No link is issued: `url` is empty. |

```json title="Declined ApproveCustomLogin response" theme={null}
{
  "decline": {
    "reason": "CLIENT_OUTDATED",
    "minAppVersion": "2.37.0"
  }
}
```

`decline.minAppVersion`, when present, is the minimum that applied to the player. Show the player an update prompt in your game, with that version when you have it. A declined code stays declined, so after updating the player starts sign-in again from the webshop.

After an `ApproveCustomLogin` decline, the webshop also shows the player its `CLIENT_OUTDATED` message, naming the minimum version, as it does for a decline your server sends.

A Stash decline doesn't sign the player out. Only a decline your server sends with `DeclineCustomLogin` can (see [Sign the player out with the decline](#sign-the-player-out-with-the-decline)).

A Stash decline sends a [`LOGIN_FAILED`](https://docs.stash.gg/guides/get-started/stash-webhooks/webhook-list) webhook with `declinedBy` set to `"STASH"`.

### Sign the player out with the decline

A `DeclineCustomLogin` that carries the player's `userId` also signs the player out of the webshop on every device, as [`LogoutUser`](#sign-a-player-out) does.

* Send the player's ID in your game as `userId` in the decline. A decline without `userId` signs no one out.
* The sign-out is best effort: the decline succeeds even when the player can't be signed out.

## Sign a player out

[`LogoutUser`](https://docs.stash.gg/api/ingress/webshop-account-linking/logoutuser) (`POST /sdk/server/custom_login/logout`) signs the player out of the webshop on every device. It also refuses every login code and [pre-authenticated link](https://docs.stash.gg/guides/stash-webshop/player-authentication/authentication-methods.md#pre-authenticated-links) issued for that player before the call, including a code you approved a moment earlier.

Before you call `LogoutUser`, check whether one of these cases fits:

| To | Do this |
| :- | :- |
| Sign out a player your catalog refuses | When your [`GetCatalog`](https://docs.stash.gg/api/egress/catalog/getcatalog) endpoint refuses a signed-in player with HTTP `401` and a JSON body whose `code` is `"PLAYER_LOGGED_OUT"`, the webshop signs that player out. You don't need to also call `LogoutUser`. |
| Refuse this login | Call [`DeclineCustomLogin`](#decline-a-login) with a `reason` instead of approving the code and then calling `LogoutUser`. The webshop then tells the player why. A decline that carries `userId` also [ends the player's other webshop sessions](#sign-the-player-out-with-the-decline), so you don't need to also call `LogoutUser`. Without `userId`, also call `LogoutUser` to end them. |

> [!WARNING]
>
> Don't call `LogoutUser` after `ApproveCustomLogin` for a login that should succeed. The logout refuses the code you just approved, and the player isn't signed in.

## Set up account linking in Unity

Use the Stash SDK to integrate account linking in Unity.

The SDK is optional. It provides convenient wrappers around the API endpoints, but you can also implement account linking with direct API calls.

### Import the Stash SDK

Follow these steps to add the Stash SDK to your Unity project:

**1. Download the SDK**

[Download the latest Stash for Unity release](https://github.com/stashgg/stash-unity).

**2. Import the package**

Import the `.unitypackage` file into your game with the [Unity asset package import process](https://docs.unity3d.com/Manual/AssetPackagesImport.html).

**3. Import demo scenes (Optional)**

Select the `Scenes` folder to import demo scenes.

### Set up deep links

See the [Unity deep linking overview](https://docs.unity3d.com/Manual/deep-linking.html) for platform-specific instructions.

Let's take a look at the structure of the Stash's deep links.

```text title="Deep Link Structure" theme={null}
stashggsample://login?code=<code>
```

The key element in the link is the code. Configure deep links correctly and add logic to extract the code when your game launches through a deep link.

### Extract the code

With deep linking configured, you can extract the code.

Use Unity's [`Application.deepLinkActivated`](https://docs.unity3d.com/Manual/deep-linking.html) event to handle deep link activations. This event triggers whenever the game launches or resumes from a Stash deep link.

In the `onDeepLinkActivated` handler, split the link to extract the code.

```csharp title="Extract Code from Deep Link" theme={null}
public void onDeepLinkActivated(string url) {
  // Extract the code parameter from the link.
  var code = url.Split("login?code=")[1];
  if (!string.IsNullOrEmpty(code)) {
    // Work with code...
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.