# Initialize services to handle the startup sequence

> Initialize Unity Gaming Services, Authentication and In-App Purchasing (IAP) so you can integrate with a Direct to Consumer (D2C) payment provider.

Create a script to initialize Unity Gaming Services (UGS) to handle the startup sequence for IAP. This startup script can do the following:

1. Initialize Unity Gaming Services.
2. Set up Authentication event handlers.
3. Sign in.
4. Initialize IAP and fetch the Remote Catalog. For more information, refer to [Fetch your Remote Catalog](./fetch-remote-catalog.md).

> **Note:**
>
> [Set your environment](workflow#configure-your-environment-in-the-unity-editor) before you initialize UGS.

In your initialization script, ensure you assign the `PurchaseManager` component in the Inspector. The `PurchaseManager` component is created as part of the next [fetch your Remote Catalog](./fetch-remote-catalog.md) step.

## Initialize services example script

The following example script is named `ServiceOrchestrator`:

> **Important:**
>
> The recommended best practice is to not use anonymous sign-in as your authentication method, because this wouldn't persist purchases after the session token is lost. If frictionless [platform-specific providers](/authentication/approaches-to-authentication.md#platform-specific-authentication) are not viable for your app, refer to [anonymous authentication and linking](/authentication/anonymous-auth-and-linking.md).

```csharp
using System;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
// Ensure you have reference to your PurchaseManager namespace if applicable

public class ServiceOrchestrator : MonoBehaviour
{
    // Assign this in the Inspector
    public PurchaseManager purchaseManager;

    async void Awake()
    {
        try
        {
            // 1. Initialize Unity Gaming Services
            await Unity.Services.Core.UnityServices.InitializeAsync();


            // 2. Setup Auth Event Handlers
            SetupEvents();


            // 3. Sign In
            await SignUpAnonymouslyAsync();


            // 4. Initialize IAP and Fetch Catalog
            await purchaseManager.InitializeIAP();
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }

    // Setup authentication event handlers if desired
    void SetupEvents() 
    {
        AuthenticationService.Instance.SignedIn += () => {
            // Shows how to get a playerID
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
            // Shows how to get an access token
            Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");
        };

        AuthenticationService.Instance.SignInFailed += (err) => {
            Debug.LogError(err);
        };

        AuthenticationService.Instance.SignedOut += () => {
            Debug.Log("Player signed out.");
        };

        AuthenticationService.Instance.Expired += () => {
            Debug.Log("Player session could not be refreshed and expired.");
        };
    }

    // Sign in
    async Task SignUpAnonymouslyAsync()
    {
        try
        {
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Sign in anonymously succeeded!");
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
        }
        catch (AuthenticationException ex)
        {
            // Compare error code to AuthenticationErrorCodes
            Debug.LogException(ex);
        }
        catch (RequestFailedException ex)
        {
            // Compare error code to CommonErrorCodes
            Debug.LogException(ex);
        }
    }
}
```

## Next steps

This page is part of a workflow to set up D2C payment providers with IAP. To continue this workflow, choose one of the following options:

[Integrate D2C payment providers](./workflow.md#initialize-services-to-handle-the-start-up-sequence): Return to the Integrate D2C payment providers with IAP workflow page.
[Fetch your Remote Catalog](./fetch-remote-catalog.md): Proceed to the next step in the workflow to set up your D2C payment provider.
