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

# User Tokens

> Initialize embedded Link for a specific existing user without exposing API credentials.

> [!NOTE]
>
> User tokens are required for [embedded Link initializations](https://docs.argyle.com/link/initialization.md) that use the `userToken` parameter.
>
> For [payroll verification workflows](https://docs.argyle.com/verifications/overview.md) that create a session, use the returned session `link` as the `connectUrl` parameter for embedded Link, or launch it directly as [Hosted Link](https://docs.argyle.com/link/initialization/hosted-link.md).

## Benefits of user tokens

* Return an existing user to embedded Link without exposing API credentials.
* Send the user directly to an existing account for [reconnection](https://docs.argyle.com/workflows/reconnecting-accounts.md) or connection management.
* Reuse the same Argyle user record instead of creating another user for a returning person.

![User tokens support embedded Link reconnect and returning-user flows.](https://mintcdn.com/argyle/9PhdnG31IuqV0CSB/images/link/user-tokens/1-users-tokens.png?fit=max\&auto=format\&n=9PhdnG31IuqV0CSB\&q=85\&s=4747781e10cc66e98f8e81c947eac427)

## Creating a user token

To prevent your API key and secret from being exposed on the front-end, request user tokens on your server side.

> [!NOTE]
>
> As a best practice, we recommend creating a new user token every time Link is initialized with a `userToken`. Please note the length of user tokens and their [decoded values](https://docs.argyle.com/link/user-tokens.md#decoded-tokens) are subject to change.

### New users

1. Create a new user by sending a **POST** request to the API's [`/users`](https://docs.argyle.com/api-reference/users) endpoint.
2. The response payload will include an `id` and `user_token`.
3. Store the `id` for creating user tokens when the same user returns.
4. Initialize Link by passing the `user_token` as the value for the `userToken` parameter.

### Returning users

1. Send a **POST** request to the API's [`/user-tokens`](https://docs.argyle.com/api-reference/user-tokens) endpoint and include the `id` of the user in the request body as a JSON object in the format `{"user": "<id>"}`.
2. A `user_token` will be included in the response payload.
3. Initialize Link by passing the `user_token` as the value for the `userToken` parameter.

## Example Link initializations

```html Web theme={}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
</head>

<body>
    <script src="https://plugin.argyle.com/argyle.web.v5.js"></script>
    <script type="text/javascript">
        const linkInstance = Argyle.create({
            userToken: 'USER_TOKEN',
            sandbox: true // Set to false for production environment.
        })
        linkInstance.open()
    </script>
</body>

</html>
```

```swift iOS theme={}
var config = LinkConfig(
    userToken: "USER_TOKEN",
    sandbox: true // Set to false for production environment.
)

ArgyleLink.start(from: viewController, config: config)
```

```kotlin Android theme={}
val config = LinkConfig(
    userToken = "USER_TOKEN",
    sandbox = true // Set to false for production environment.
)

ArgyleLink.start(context, config)
```

```js React Native theme={}
import { ArgyleLink } from '@argyleio/argyle-plugin-react-native';

// ...

const config = {
    userToken: 'USER_TOKEN',
    sandbox: true // Set to false for production environment.
};

ArgyleLink.start(config);
```

```dart Flutter theme={}
import 'package:argyle_link_flutter/link_config.dart';

// ...

final config = LinkConfig (
    userToken: 'USER_TOKEN',
    sandbox: true // Set to false for production environment.
);

ArgyleLink.start(config);
```

## Returning to connected accounts

When just a `userToken` is included in your Link initialization, a returning user that has already connected a payroll account will initially arrive at Link's home screen. From there, the user can search for and connect additional accounts, or access their already-connected accounts by selecting "Your connections."

Including both the required `userToken` and optional `accountId` [Link initialization parameters](https://docs.argyle.com/link/initialization/overview.md#optional-initialization-parameters) opens the existing payroll account that matches `accountId`. This skips the **Your connections** selection step for actions such as:

* [Reconnecting](https://docs.argyle.com/workflows/reconnecting-accounts.md) a disconnected account
* [Revoking](https://docs.argyle.com/workflows/reconnecting-accounts.md#revoking-access) account access

![Combine userToken and accountId in your Link initialization to directly connect a user to an existing payroll account connected through Argyle.](https://mintcdn.com/argyle/9PhdnG31IuqV0CSB/images/link/user-tokens/2-users-tokens.png?fit=max\&auto=format\&n=9PhdnG31IuqV0CSB\&q=85\&s=df25fb016d5eaf8fc06e7c914c324992)

## Decoded tokens

Argyle utilizes [JWT tokens](https://jwt.io/) for user tokens.

> [!NOTE]
>
> For troubleshooting, JWT tokens can be decoded. However, we do not recommend monitoring individual token fields such as expiry date, and instead recommend creating a new user token every time Link is initialized with a `userToken` as a best practice.

Decoded JWT token payload

```json theme={}

{
    "client_id": "0d9b5bf3-97fa-4757-a136-b2a03d171414",
    "exp": 1652481485,
    "iat": 1649889485,
    "iss": "argyle-core-auth-prod",
    "jti": "00097a26-2f2a-4aa0-8eca-95ebe56d57a8",
    "sub": "017f8978-bbfd-ff64-18ce-d59f99bf51c2",
    "user_id": "017f8978-bbfd-ff64-18ce-d59f99bf51c2"
}

```
