SDK initialization

How to start Purchasely SDKs and all necessary parameters

Start

To ensure that Purchasely is ready as soon as possible, we advise starting the SDK immediately when your application launches. Purchasely will initialize in a background thread to ensure that your application launch time and user experience are not affected.

import Purchasely

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Purchasely.start(
       withAPIKey: "<<X-API-KEY>>",
       appUserId: nil, // optional if you already know your user id
       logLevel: .debug
    ) {(success, error) in
      print(success)
    }
	return true
}
import android.app.Application
import io.purchasely.ext.Purchasely
import io.purchasely.google.GoogleStore

class YourApplication: Application() {

    override fun onCreate() {
        super.onCreate()

        Purchasely.Builder(applicationContext)
            .apiKey("<<X-API-KEY>>")
            .userId(null) // optional if you already know your user id
            .build()
            .start { isConfigured, error ->
                if(isConfigured) {
                    // Purchasely setup is complete 
                )
            }
    }
}
import Purchasely from 'react-native-purchasely';

// Everything is optional except apiKey and storeKit1
// Example with default values
try {
    const configured = await Purchasely.start({
        apiKey: '<<X-API-KEY>>',
        logLevel: LogLevels.ERROR, // set to debug in development mode to see logs
        userId: null // if you know your user id, set it here
     });
} catch (e) {
     console.log("Purchasely SDK not configured properly");
}
// Everything is optional except apiKey and storeKit1
// Example with default values
bool configured = await Purchasely.start(
        apiKey: '<<X-API-KEY>>',
        logLevel: PLYLogLevel.error, // set to debug in development mode to see logs
        userId: null, // set a user id if you have one
      );
    
if (!configured) {
        print('Purchasely SDK not configured');
        return;
}
/**
* @params String apiKey
* @params StringArray stores : may be Google, Amazon and Huawei
* @params String userId
* @params Purchasley.LogLevel logLevel
* @params Purchasely.RunningMode runningMode
**/
Purchasely.startWithAPIKey(
    '<<X-API-KEY>>', 
    ['Google'],
    null, // user id of user
    Purchasely.LogLevel.DEBUG 
);

More details on the SDK running modes.

📘

This call is mandatory

Ensure that Purchasely.start() is the first method executed by your application.
This process does not block the main thread, allowing you to call other SDK methods immediately after invoking this method.


API Key

The API Key serves as a confidential identifier, enabling your application to authenticate with Purchasely. It's crucial to securely store this key within your application and ensure it is never disclosed publicly.

You can find your API Key in the section App settings / Backend & SDK configuration of the Purchasely Console and copy it by clicking on the Copy button.


User identification

There are 2 types of users in Purchasely platform

  • connected user
  • anonymous user

You can provide a user id with Purchasely.start() if the user is already signed-in or use Purchasely.userLogin() if the user signs in after your application has started.

You can find more information about users in the dedicated section

Callback on initialization

You can provide a callback method when you start the SDK. It will be automatically called when the SDK has finished initializing.

You can display a screen without waiting the SDK to be fully initialized.

The callback returns two values:

  • success: true | false to indicate whether the SDK was initialized successfully and whether the configuration is correct. If it returns false, you can still use Purchasely SDK.
  • error: Indicates the specific error that may have occurred when the success value is false.


What’s Next

You can look at how to use deeplinks with Purchasely or skip directly to displaying screens