SDKs overview
Klarna provides software development kits (SDKs) and libraries designed to simplify the integration of its payment and financing services into various applications and platforms. These SDKs abstract the underlying RESTful API interactions, allowing developers to implement Klarna's payment options, such as Pay in 4 or Pay in 30 days, with reduced development time. Klarna's developer documentation serves as the central resource for all SDKs and API specifications.
The SDKs are available for both server-side and client-side implementations, supporting a range of programming languages and mobile platforms. The primary goal of these libraries is to provide a consistent and secure method for handling transactions, managing orders, and offering Klarna's consumer financing options within a merchant's digital storefront.
Official SDKs by language
Klarna maintains official SDKs for several popular programming languages and platforms. These SDKs are developed and supported by Klarna to ensure compatibility with their API and adherence to best practices for security and performance. The table below outlines the key official SDKs, their corresponding package names, and typical installation commands.
| Language/Platform | Package/Repository | Install Command (Example) | Maturity |
|---|---|---|---|
| JavaScript (Web) | Klarna Checkout SDK (Embedded) | <script src="https://static.klarna.com/checkout/widget-v1/klarna-ui.js"></script> |
Stable |
| PHP | klarna/kco_rest |
composer require klarna/kco_rest |
Stable |
| Python | klarna-kco |
pip install klarna-kco |
Stable |
| Ruby | klarna-kco |
gem install klarna-kco |
Stable |
| Java | com.klarna.rest:klarna-rest-sdk |
Add to Maven/Gradle dependencies | Stable |
| Android | Klarna Mobile SDK | Add to Gradle dependencies | Stable |
| iOS | Klarna Mobile SDK (CocoaPods) | pod 'KlarnaMobileSDK' |
Stable |
| .NET | Klarna.Kco |
Install-Package Klarna.Kco |
Stable |
For detailed documentation and version-specific installation instructions, developers should refer to the Klarna API documentation portal.
Installation
Installation procedures vary based on the programming language and target platform. Below are general installation guidelines for common environments. For precise instructions, consult the official Klarna API reference for your specific SDK.
JavaScript (Web)
For web integrations, the Klarna Checkout SDK is often embedded directly into the HTML of your checkout page. This typically involves including a script tag that loads Klarna's client-side JavaScript library.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Klarna Checkout</title>
<script src="https://static.klarna.com/checkout/widget-v1/klarna-ui.js"></script>
</head>
<body>
<!-- Your checkout content -->
</body>
</html>
PHP
PHP SDKs are typically installed via Composer, the dependency manager for PHP.
composer require klarna/kco_rest
Python
Python SDKs are installed using pip, the Python package installer.
pip install klarna-kco
Ruby
Ruby SDKs are installed using RubyGems, Ruby's package manager.
gem install klarna-kco
Java
Java SDKs are managed using build automation tools like Maven or Gradle. Add the dependency to your project's pom.xml (Maven) or build.gradle (Gradle).
Maven (pom.xml)
<dependency>
<groupId>com.klarna.rest</groupId>
<artifactId>klarna-rest-sdk</artifactId>
<version>X.Y.Z</version> <!-- Replace with the latest version -->
</dependency>
Gradle (build.gradle)
implementation 'com.klarna.rest:klarna-rest-sdk:X.Y.Z' // Replace with the latest version
Android (Kotlin/Java)
For Android applications, integrate the Klarna Mobile SDK by adding it as a dependency in your app's build.gradle file.
dependencies {
implementation 'com.klarna.mobile.sdk:klarna-mobile-sdk:X.Y.Z' // Replace with the latest version
}
iOS (Swift/Objective-C)
For iOS applications, the Klarna Mobile SDK is typically installed via CocoaPods or Swift Package Manager.
CocoaPods (Podfile)
pod 'KlarnaMobileSDK', '~> X.Y.Z' # Replace with the latest version
Quickstart example
This quickstart example demonstrates a basic server-side order creation using the Python SDK. This initiates a Klarna Checkout session, which then renders payment options to the user on the frontend.
Before running, ensure you have your Klarna API credentials (Merchant ID and Shared Secret) for the test or live environment. These are obtained from the Klarna Merchant Portal, as outlined in the Klarna Getting Started guide.
from klarna_kco import KlarnaCheckout
# Replace with your actual Klarna Merchant ID and Shared Secret
MERCHANT_ID = 'YOUR_MERCHANT_ID'
SHARED_SECRET = 'YOUR_SHARED_SECRET'
# Configure KlarnaCheckout client for a test environment (US region example)
# For other regions, adjust the 'region' parameter (e.g., 'eu' for Europe)
checkout_client = KlarnaCheckout(
merchant_id=MERCHANT_ID,
shared_secret=SHARED_SECRET,
test_mode=True, # Set to False for production
region='us' # 'eu' or 'oc' for other regions
)
# Define the order payload as per Klarna API specifications
order_payload = {
'purchase_country': 'US',
'purchase_currency': 'USD',
''locale': 'en-US',
'order_amount': 10000, # 100.00 USD (in cents)
'order_tax_amount': 2000, # 20.00 USD (in cents)
'order_lines': [
{
'type': 'physical',
'reference': '123050',
'name': 'Klarna T-Shirt',
'quantity': 1,
'unit_price': 8000, # 80.00 USD (in cents)
'total_amount': 8000,
'total_tax_amount': 1600,
'tax_rate': 2000 # 20% (in basis points)
},
{
'type': 'shipping_fee',
'reference': 'shipping',
'name': 'Shipping Fee',
'quantity': 1,
'unit_price': 2000, # 20.00 USD (in cents)
'total_amount': 2000,
'total_tax_amount': 400,
'tax_rate': 2000 # 20% (in basis points)
}
],
'merchant_urls': {
'checkout': 'https://example.com/checkout',
'confirmation': 'https://example.com/confirmation?klarna_order_id={checkout.order.id}',
'push': 'https://example.com/klarna-push-callback'
}
}
try:
# Create a new Klarna Checkout order
created_order = checkout_client.create_order(order_payload)
print(f"Klarna Order Created: {created_order['order_id']}")
print(f"Redirect URL: {created_order['redirect_url']}")
# The 'html_snippet' contains the Klarna Checkout iframe content to embed on your checkout page
print(f"HTML Snippet (for embedding):\n{created_order['html_snippet']}")
except Exception as e:
print(f"Error creating Klarna order: {e}")
Community libraries
Beyond the official SDKs, the developer community often contributes libraries, plugins, and integrations for various platforms and frameworks. While not officially supported by Klarna, these resources can offer solutions for niche use cases or accelerate development in specific environments. Examples include:
- E-commerce Platform Plugins: Many popular e-commerce platforms like Shopify, Magento, WooCommerce, and PrestaShop have official Klarna plugins or community-developed modules that leverage Klarna's APIs. These are typically listed on the respective platform's marketplace or Klarna's business solutions pages. For instance, see Shopify's Klarna integration guide.
- Framework-Specific Adapters: Developers might create wrappers or adapters for specific web frameworks (e.g., Django, Flask, Ruby on Rails) to simplify integration with Klarna's services beyond the core SDK functionalities.
- API Clients: Sometimes, community members develop alternative API clients in languages not officially covered by Klarna or provide different architectural approaches to integration.
When considering community-contributed libraries, it is important to review their maintenance status, security practices, and compatibility with the latest Klarna API versions. Developers should exercise due diligence and consult the secure coding guidelines for third-party integrations, as recommended by organizations like the Mozilla Developer Network.