SDKs overview
Klarna offers a suite of Software Development Kits (SDKs) designed to facilitate the integration of its payment and financing services into various applications and e-commerce platforms. These SDKs abstract the complexity of direct API interactions, providing developers with pre-built functionalities to implement Klarna's payment options, such as 'Pay in 4' or 'Pay in 30 days', directly into their checkout flows. The SDKs are maintained to ensure compatibility with Klarna's evolving API landscape and adhere to security standards, including GDPR compliance Klarna's GDPR compliance details.
The primary goal of Klarna's SDKs is to reduce development time and effort required for merchants to offer flexible payment solutions to their customers. By leveraging these libraries, developers can implement Klarna's services, manage orders, handle refunds, and retrieve order details without needing to manage raw HTTP requests or complex data serialization. The SDKs typically include components for authentication, request building, response parsing, and error handling, aligning with best practices for API consumption.
Klarna's developer portal Klarna developer documentation serves as the central hub for all SDK-related resources, including detailed API references, integration guides, and examples. A sandbox environment is also available, allowing developers to test their integrations thoroughly before deploying to a live production environment.
Official SDKs by language
Klarna provides official SDKs for several popular programming languages, ensuring broad compatibility across different development environments. These SDKs are actively maintained by Klarna and are the recommended method for integrating Klarna's services. Each SDK is designed to be idiomatic to its respective language, offering a natural development experience.
| Language | Package/Repository | Maturity | Documentation Link |
|---|---|---|---|
| JavaScript | @klarna/js-sdk |
Stable | Klarna JavaScript SDK |
| PHP | klarna/kco_rest |
Stable | Klarna PHP SDK |
| Python | klarna_rest |
Stable | Klarna Python SDK |
| Ruby | klarna_rest |
Stable | Klarna Ruby SDK |
| Java | com.klarna.rest |
Stable | Klarna Java SDK |
| C# | Klarna.Rest |
Stable | Klarna C# SDK |
| Node.js | klarna-rest |
Stable | Klarna Node.js SDK |
Installation
Installing Klarna's SDKs typically involves using the standard package managers for each respective language. The following commands provide a general overview; specific versions and additional dependencies can be found in the official documentation for each SDK.
JavaScript (NPM)
npm install @klarna/js-sdk
PHP (Composer)
composer require klarna/kco_rest
Python (pip)
pip install klarna-rest
Ruby (Bundler/Gem)
gem install klarna_rest
Alternatively, add to your Gemfile:
gem 'klarna_rest'
bundle install
Java (Maven/Gradle)
For Maven, add to your pom.xml:
<dependency>
<groupId>com.klarna.rest</groupId>
<artifactId>klarna-rest-client</artifactId>
<version>X.Y.Z</version> <!-- Replace with latest version -->
</dependency>
For Gradle, add to your build.gradle:
implementation 'com.klarna.rest:klarna-rest-client:X.Y.Z' // Replace with latest version
C# (.NET CLI/NuGet)
dotnet add package Klarna.Rest
Alternatively, via NuGet Package Manager Console:
Install-Package Klarna.Rest
Node.js (NPM)
npm install klarna-rest
Quickstart example
This quickstart example demonstrates how to initialize the Klarna Payments API client and create a new order using the Python SDK. This snippet assumes you have your Klarna API credentials (Merchant ID and Shared Secret) ready for a test environment. For production environments, ensure secure handling of credentials, potentially using environment variables or a secrets management service Google Cloud Secret Manager overview.
from klarna_rest import Client, ApiException
from klarna_rest.models.payments_create_session_request_dto import PaymentsCreateSessionRequestDto
from klarna_rest.models.order_line_item_dto import OrderLineItemDto
# Klarna API credentials (use your test credentials for sandbox)
MERCHANT_ID = "YOUR_MERCHANT_ID"
SHARED_SECRET = "YOUR_SHARED_SECRET"
# Initialize Klarna client for EU (can be US/OC for other regions)
client = Client(MERCHANT_ID, SHARED_SECRET, "eu") # 'eu', 'us', or 'oc'
# Prepare order line items
order_lines = [
OrderLineItemDto(
name="Klarna T-Shirt",
quantity=1,
unit_price=1200, # 12.00 EUR (1200 cents)
total_amount=1200,
type="physical_product"
),
OrderLineItemDto(
name="Shipping Fee",
quantity=1,
unit_price=500, # 5.00 EUR
total_amount=500,
type="shipping_fee"
)
]
# Create a new payments session request
session_request = PaymentsCreateSessionRequestDto(
purchase_country="DE",
purchase_currency="EUR",
locale="en-DE",
order_amount=1700, # Total order amount: 12.00 + 5.00 = 17.00 EUR
order_lines=order_lines,
merchant_urls={
"terms": "https://www.example.com/terms",
"checkout": "https://www.example.com/checkout",
"confirmation": "https://www.example.com/confirmation",
"push": "https://www.example.com/push"
}
)
try:
# Create a new payments session
session_response = client.payments.create_session(session_request)
print(f"Created Klarna Payments Session ID: {session_response['session_id']}")
print(f"Client Token: {session_response['client_token']}")
# The client_token is used on the frontend to render the Klarna widget.
# Store session_id for future reference (e.g., order management).
except ApiException as e:
print(f"Error creating Klarna Payments session: {e}")
print(f"Error response body: {e.body}")
This example initializes the Klarna client with merchant credentials and then constructs a request to create a payment session. The client_token returned by the API is crucial for rendering the Klarna checkout widget on the client-side, enabling customers to select their preferred payment method. The session_id uniquely identifies the payment session and is used for subsequent API calls related to that specific order.
Community libraries
While Klarna provides a robust set of official SDKs, the developer community sometimes contributes additional libraries or plugins, particularly for e-commerce platforms or niche use cases not directly covered by official offerings. These community-driven projects can range from framework-specific integrations (e.g., for Django, Ruby on Rails) to specialized UI components.
It is important to exercise caution when using community-maintained libraries. Factors to consider include:
- Maintenance Status: Is the library actively maintained and updated to reflect the latest Klarna API changes and security best practices?
- Security Audits: Has the library undergone any security reviews? Unofficial libraries might introduce vulnerabilities if not properly vetted.
- Documentation and Support: Is the documentation comprehensive, and is there an active community for support?
- Compatibility: Is the library compatible with the specific version of Klarna's API you intend to use?
Developers are encouraged to consult Klarna's official documentation and developer forums for recommendations on third-party integrations. For critical production systems, relying on official SDKs or well-supported, widely adopted community projects is generally advisable to ensure stability and security. Examples of community contributions often appear as plugins for popular e-commerce platforms like WooCommerce, Shopify, or Magento, which often have their own ecosystems for extensions.
For custom integrations or when an official SDK is not available for a specific language or platform, developers can interact directly with Klarna's RESTful API Klarna API reference. This approach requires more manual handling of HTTP requests, JSON serialization, and error management but offers maximum flexibility. Adhering to the API reference ensures direct compatibility and access to all available features.