Authentication overview

Klarna's API authentication system is designed to secure transactions and merchant data by ensuring that only authorized entities can access and interact with its services. The primary method for authenticating API requests to Klarna's various endpoints, including those for Payments, Orders, and Customer data, is HTTP Basic Authentication. This mechanism requires developers to use a unique set of credentials—a Merchant ID and an API Password—obtained from their Klarna Merchant Portal account.

All communication with Klarna's API must occur over HTTPS/TLS to encrypt data in transit, protecting sensitive information from interception. This standard practice aligns with industry best practices for securing web communications, as detailed in the IETF's HTTP Authentication specification. Developers integrating Klarna can utilize various Klarna SDKs, which abstract away some of the complexities of handling authentication headers and request formatting, streamlining the integration process.

Supported authentication methods

Klarna primarily supports HTTP Basic Authentication for its API interactions. This method is straightforward to implement and widely supported across different programming languages and environments. While Basic Authentication is the core method, the secure transmission over HTTPS is crucial for its effectiveness.

Method When to Use Security Level
HTTP Basic Authentication (Merchant ID & Password) Direct API calls, server-side integrations, SDK usage High (when combined with HTTPS/TLS encryption)
Klarna Checkout (Client-side integration) Embedding Klarna's checkout experience directly into a web page Managed by Klarna; relies on secure server-side session creation

HTTP Basic Authentication involves encoding the merchant ID and API password in a base64 string and including it in the Authorization header of each API request. The format is Authorization: Basic <base64_encoded_credentials>, where <base64_encoded_credentials> is derived from merchant_id:api_password. This is a standard approach for API authentication, as described in the MDN Web Docs on HTTP authentication.

Getting your credentials

To obtain the necessary authentication credentials for Klarna's APIs, merchants must have an active Klarna business account. The process typically involves the following steps:

  1. Sign Up/Log In to Klarna Merchant Portal: Access the Klarna Merchant Portal using your business account credentials. This portal is the central hub for managing your Klarna services.
  2. Navigate to API Credentials Section: Within the portal, locate the section dedicated to API credentials or developer settings. The exact path may vary but is generally found under 'Settings', 'Integrations', or 'Developer Resources'.
  3. Generate API Credentials: Follow the instructions to generate a new set of API credentials. This usually involves creating a new API user or key, which will provide you with a unique Merchant ID and an API Password. It is critical to store these credentials securely immediately after generation, as the API password may only be displayed once.
  4. Distinguish Test and Live Credentials: Klarna provides separate environments for testing (sandbox) and live transactions. Ensure you generate and use the appropriate credentials for your current development stage. Using live credentials in a test environment or vice-versa will result in authentication failures.

Refer to the Klarna API documentation for the most up-to-date and specific instructions on credential generation for your region and account type.

Authenticated request example

Here's an example of an authenticated API request using curl, demonstrating HTTP Basic Authentication. Replace YOUR_MERCHANT_ID and YOUR_API_PASSWORD with your actual credentials.

# Replace with your actual Merchant ID and API Password
MERCHANT_ID="YOUR_MERCHANT_ID"
API_PASSWORD="YOUR_API_PASSWORD"

# Encode credentials in Base64
AUTH_STRING=$(echo -n "${MERCHANT_ID}:${API_PASSWORD}" | base64)

# Example: Create an order (Payments API)
curl -X POST \
  "https://api.klarna.com/payments/v1/sessions" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Authorization: Basic ${AUTH_STRING}" \
  -d '{ 
    "order_amount": 25000, 
    "order_tax_amount": 5000, 
    "purchase_country": "US", 
    "purchase_currency": "USD", 
    "locale": "en-US", 
    "order_lines": [
      {
        "type": "physical",
        "name": "Klarna T-Shirt",
        "quantity": 1,
        "unit_price": 20000,
        "total_amount": 20000,
        "total_tax_amount": 4000
      },
      {
        "type": "shipping_fee",
        "name": "Shipping",
        "quantity": 1,
        "unit_price": 5000,
        "total_amount": 5000,
        "total_tax_amount": 1000
      }
    ],
    "merchant_urls": {
      "terms": "https://www.example.com/terms.html",
      "checkout": "https://www.example.com/checkout.html",
      "confirmation": "https://www.example.com/confirmation.html",
      "push": "https://www.example.com/push"
    }
  }'

This curl command demonstrates how the Authorization header is constructed and sent along with the request body to Klarna's Payments API to create a new session. The specific endpoint and request body will vary depending on the Klarna API product you are interacting with (e.g., Payments, Orders, Settlements).

Security best practices

Implementing strong security practices is essential when integrating with Klarna's APIs to protect sensitive payment and customer data. Adhering to these guidelines helps prevent unauthorized access and maintain compliance:

  • Keep Credentials Confidential: Treat your Merchant ID and API Password as highly sensitive information. Never hardcode them directly into client-side code, commit them to public version control systems, or expose them in browser-accessible JavaScript. Store them in secure environment variables, a secrets management service, or a secure configuration file on your server.
  • Use HTTPS/TLS for All API Calls: Always ensure that all communication with Klarna's API endpoints is encrypted using HTTPS/TLS. This protects your credentials and transaction data from eavesdropping during transit. Klarna's APIs enforce HTTPS, so attempts to connect via HTTP will fail.
  • Rotate API Passwords Regularly: Periodically change your API passwords. This practice limits the window of exposure if credentials are ever compromised. Check the Klarna Merchant Portal for options to rotate or regenerate API keys.
  • Implement Least Privilege: If Klarna offers different API roles or scopes (e.g., read-only vs. read/write), configure your API credentials with the minimum necessary permissions required for your application's functionality. This reduces the potential impact of a compromised key.
  • Secure Your Server Environment: Ensure that the server environment hosting your application is secure. This includes regular security updates, firewalls, intrusion detection systems, and restricted access to sensitive files and directories.
  • Monitor for Unauthorized Access: Implement logging and monitoring for API access and authentication attempts. Unusual activity (e.g., numerous failed login attempts, requests from unexpected IP addresses) should trigger alerts for investigation.
  • Client-Side Security for Klarna Checkout: When using Klarna Checkout, ensure your client-side integration follows Klarna's guidelines to prevent cross-site scripting (XSS) and other client-side vulnerabilities that could compromise the checkout experience.
  • Comply with Data Protection Regulations: Adhere to relevant data protection regulations such as GDPR when handling customer data, especially when integrating payment solutions.