# Interface implementation-based token generation

> Learn about interface implementation-based token generation.

If you want to control how Vivox Access Tokens (VATs) are generated, create a class that implements the Vivox SDK’s `IVivoxTokenProvider` interface and register it with the VivoxService.

When you register your implementation of IVivoxTokenProvider by calling `VivoxService.Instance.SetTokenProvider`, Vivox will call your implementation's `IVivoxTokenProvider.GetTokenAsync` method whenever a token is needed for actions such as logging in.

The information needed to create a payload is provided as input to the overridden `IVivoxTokenProvider.GetTokenAsync` method. This information is used when crafting a VAT.

You will still need to set up server-side token vending and fetch the token within the overridden `IVivoxTokenProvider.GetTokenAsync` method.

## To subscribe

Before initializing the Vivox SDK, you must register your `IVivoxTokenProvider` implementation using:

```text
VivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
```

## Fetch the token

Create the payload with all of the parameters provided in the overridden method, even if some are empty, and send it to your secure server to generate your Vivox Access Token. Best practice is to send all the parameters; only what’s needed for the payload will be returned. Use the payload as input into the `GetTokenAsync` method.

For certain tokens, different parameters will be empty. For example, targetUserUri and channelUri will be empty for a login token. This is expected behaviour.

The following is an example of token generation:

```cs
public class VoiceManager : MonoBehaviour
{
    async void Start()
    {
        // Must be done before any other Vivox action otherwise tokens will not be generated properly.
        VivoxService.Instance.SetTokenProvider(new VivoxTokenProvider());
        await UnityServices.InitializeAsync();
        await VivoxService.Instance.InitializeAsync();
    }
}

class VivoxTokenProvider : IVivoxTokenProvider
{
    public Task<string> GetTokenAsync(string issuer = null, TimeSpan? expiration = null, string targetUserUri = null, string action = null, string channelUri = null, string fromUserUri = null, string realm = null)
    {
        // Implement token fetching logic here.
        // The method parameters contain the necessary information for crafting the request payload.
        // This will be called whenever a token is needed for a Vivox action
    }
}
```

To learn more about generating server-side tokens, refer to [Generate a token on a secure server](./generate-token-secure-server/generate-token-secure-server-toc).
