Klarna Docs - Sign in with Klarna

Sign in with Klarna

In this step-by-step guide you will learn how to integrate Sign in with Klarna into your Android app.

Before starting to add the Sign in with Klarna button or using the Sign in with Klarna SDK you will need to do two things:

  1. Setting the return URL
  2. Defining an event handler

1. Setting the Return URL

Add this snippet to your manifest file in order to set your return URL value for the SDK’s activity.

MARKUP
<activity android:name="com.klarna.mobile.sdk.activity.KlarnaRedirectReceiverActivity" android:exported="true" tools:node="replace">
   <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="<YOUR-SCHEME>" android:host="<YOUR-HOST>"/>
   </intent-filter>
</activity>

In version 2.6.11 and later, a new activity called `KlarnaRedirectReceiverActivity` has been introduced in the Android SDK.

When setting the returnURL parameter for KlarnaSignInButton or KlarnaSignInSDK , you need to concatenate the scheme and host you specified in AndroidManifest.xml into one value using :// as the separator, as shown here.

KOTLIN
val button = KlarnaSignInButton(... returnURL =  “<YOUR-SCHEME>://<YOUR-HOST>”)

Utilizing `FLAG_ACTIVITY_NEW_DOCUMENT` when initializing an activity that proceeds to the Sign in with Klarna operation can interfere with the user navigation within the app. This flag might induce Android to spawn a new app instance upon returning to the app via the return URL deep linking, consequently disrupting the login process. To evade potential user inconveniences, we recommend avoiding `FLAG_ACTIVITY_NEW_DOCUMENT` when launching an activity that may trigger the Sign in with Klarna procedure.

2. Defining an Event Handler

While the user is interacting with the Sign in with Klarna flow or when the flow is completed, you'll receive events by setting an event handler when creating an instance of KlarnaSignInSDK. On the right you can see an example of how to implement such an event handler.

KOTLIN
val eventHandler = object : KlarnaEventHandler {
   override fun onEvent(klarnaComponent: KlarnaComponent, event: KlarnaProductEvent) {
      when(event.action) {
         KlarnaSignInEvent.USER_TAPPED_BUTTON -> {
            // User tapped the KlarnaSignInButton, auth process starting
         }
         KlarnaSignInEvent.USER_AUTH -> {
            // User completed interactive auth, tokens will be fetched
         }
         KlarnaSignInEvent.USER_CANCELLED -> {

To integrate the button for Sign in with Klarna as a native view in your Android app, you need to create an instance of it, add it to your view hierarchy, and receive the results once the flow is completed.

The button for Sign in with Klarna native view in Android is called KlarnaSignInButton. You can create an instance programmatically or inflate it from an XML layout file. You can set the parameters of the button both programatically or in the XML layout file.

Create the Button Programmatically

You can create the Sign in with Klarna button programmatically and place it in your app with desired layout options. You can see a sample on the right.

KOTLIN
//Create an instance of KlarnaSignInButton
val button = KlarnaSignInButton(...)

// Add it to your container with desired layout parameters
val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
containerViewGroup.addView(button, 0, layoutParams)

Create the Button in XML

You can also add the Sign in with Klarna button into your XML layout file and get the view instance programmatically either by findViewById or by the generated View Binding instance of your layout (if you're using View Binding).

MARKUP
 <com.klarna.mobile.sdk.api.signin.KlarnaSignInButton
      android:id="@+id/klarnaSignInButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      …
/>

You can set the parameters of the Sign in with Klarna button either by setting them on the button instance or by setting them as attributes in the XML layout file.

ParameterTypeRequiredDescription
contextContextYesThe context of the Activity the button is in.
clientIdStringYes (optional in the constructor)The UUID you get when creating your Klarna OAuth 2.0 app.The XML attribute of this parameter is app:KlarnaSignInButtonClientId.
scopeStringYes (optional in the constructor)A space-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation. The XML attribute of this parameter is app:KlarnaSignInButtonScope. The claims of the requested scopes will be returned as part of the JWT token id_token. Note: openid is always requested by default even if you don't request it explicitly.Available scopes are as follows:
  • offline_access
  • profile:email
  • profile:phone
  • profile:name
  • profile:date_of_birth
  • profile:billing_address
  • profile:national_identification
  • profile:country
  • payment:request:create
marketStringYes (optional in the constructor)The market or the country where this integration is available, for example, SE for Sweden. The XML attribute of this parameter is app:KlarnaSignInButtonMarket.
localeStringYes (optional in the constructor)The language in which the Sign in with Klarna button is presented to the user, for example, sv-SE or en-SE for Sweden. The XML attribute of this parameter is app:KlarnaSignInButtonLocale.
eventHandlerKlarnaEventHandlerNoThe interface to receive the results from the Sign in with Klarna flow.
environmentKlarnaEnvironmentNoConfigures the endpoints and other behaviors that the SDK will be operating with. When set to production, the SDK will make requests to production endpoints and perform real validation, whereas for other environments will not. The XML attribute of this parameter is app:klarnaEnvironment.
regionKlarnaRegionNoDefines the regional API endpoints to which the SDK will send requests. The XML attribute of this parameter is app:klarnaRegion.
themeKlarnaThemeNoDefines the theming for the Sign in with Klarna UI, but not the button itself. The XML attribute of this parameter is app:klarnaTheme.
returnURLStringYes (optional in the constructor)The URL you defined in your AndroidManifest.xml file in the preparation section. The XML attribute of this parameter is app:klarnaReturnUrl.
loggingLevelKlarnaLoggingLevelNoThe logging level which will be used when logging messages to the Logcat. The XML attribute of this parameter is app:klarnaLoggingLevel.
resourceEndpointKlarnaResourceEndpointNoDefines the cloud provider to which the SDK will send requests. Please note that this should not be changed or overridden. The XML attribute of this parameter is app:klarnaResourceEndpoint.

In addition the the parameters list above, Klarna offers several themes, labels, and shapes to customize the button's appearance to match the look and feel of your app.

ParameterTypeDescription
buttonThemeKlarnaButtonThemeEnumerated value to style the button in the light or dark theme.
buttonShapeKlarnaButtonShapeEnumerated value specifying the button's shape.
buttonLabelKlarnaButtonLabelEnumerated value specifying the text that will be displayed on the button.

The KlarnaButtonTheme enumeration defines which style is applied to the button, depending on either the app’s configuration or the system settings.

ValueDescription
KLARNARenders the button in the Klarna pink theme. We recommend this theme as it has the highest brand recognition and the likelihood of usage by your customers.
LIGHTRenders the button in white. We recommend this theme for dark backgrounds.
DARKRenders the button in black. We recommend this theme for light backgrounds.
AUTOAutomatic theme (light or dark) that will depend on the system’s UI theme.

In order for the button to fit into the aesthetic of your app, we offer different button shapes:

ValueDescription
ROUNDED_RECTRenders the button as a rectangle with rounded corners.
PILLRenders the button as a pill-shaped button.
RECTANGLERenders the button as a rectangle with square corners.

To integrate the Sign in with Klarna SDK in your Android app, you need to create an instance of KlarnaSignInSDK, call its signIn() function, and receive the results once the flow is completed.

To create an instance of the SDK you need to specify the following parameters:

ParameterTypeRequiredDescription
activityActivityYesThe activity instance of your app.
returnURLStringYesThe URL you defined in your AndroidManifest.xml file in the preparation section.
eventHandlerKlarnaEventHandlerYes (optional in the constructor)The interface to receive the results from the Sign in with Klarna flow.
environmentKlarnaEnvironmentNoEnumerated value to set the working environment for the SDK.
regionKlarnaRegionNoEnumerated value to set the regional API endpoints to which the SDK will send requests.
themeKlarnaThemeNoDefines the theming for the Sign in with Klarna UI, but not the button itself.
loggingLevelKlarnaLoggingLevelNoThe logging level which will be used when logging messages to the Logcat.
resourceEndpointKlarnaResourceEndpointNoDefines the cloud provider to which the SDK will send requests.

To initiate the sign in flow for the user, you need to call the signIn() function of the SDK and specify the following parameters:

ParameterTypeRequiredDescription
clientIdStringYesThe UUID you get when creating your Klarna OAuth 2.0 app.
scopeStringYesA space-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation. The claims of the requested scopes will be returned as part of the JWT token id_token. Note: openid is always requested by default even if you don't request it explicitly. Available scopes are as follows:
  • offline_access
  • profile:email
  • profile:phone
  • profile:name
  • profile:date_of_birth
  • profile:billing_address
  • profile:national_identification
  • profile:country
  • payment:request:create
marketStringYesThe market or the country where this integration is available, for example, SE for Sweden.
localeStringNoThe language to present to the user, for example, sv-SE or en-SE for Sweden.

After calling the signIn() function you will receive the events in the event handler that you've passed to the SDK.