"Six months ago, everyone was talking about MCPs. And I was like, screw MCPs. Every MCP would be better as a CLI."
— Peter Steinberger, Founder of OpenClaw Watch on YouTube (~2:39:00) | Lex Fridman Podcast #491
Production-ready command-line interface for the Klarna Payments API V1. Built with Commander.js for developers who prefer the power and simplicity of CLI tools over MCP servers.
⚠️ Unofficial CLI - This tool is not officially sponsored, endorsed, or maintained by Klarna. It is an independent project built on the public Klarna API. Official site: https://www.klarna.com | API docs: https://docs.klarna.com/api/
- No Server Overhead: Direct API calls without maintaining a persistent server process
- Zero Configuration: Just set credentials and start making API calls
- Shell Integration: Pipe, redirect, and compose with standard Unix tools
- Git-Friendly: Command history and scripts version control beautifully
- Instant Startup: No server boot time or connection handshakes
- Stateless: Each command is independent, no session state to manage
- Lightweight: Pure Node.js with minimal dependencies
- Predictable: Same inputs always produce same outputs
- Scriptable: Write bash scripts for complex workflows
- Composable: Chain with jq, grep, awk for powerful data processing
- Debuggable: Clear error messages and exit codes
- Portable: Works anywhere Node.js runs, no additional infrastructure
- Deterministic: AI agents get consistent, structured output
- Error Handling: Proper exit codes for easy success/failure detection
- JSON Output: Machine-readable responses for parsing
- Documentation: Clear help text and examples for LLM context
npm install -g @ktmcp-cli/klarnanpm install @ktmcp-cli/klarna
npx klarna --helpgit clone <repository>
cd klarna-cli
npm install
npm linkexport KLARNA_USERNAME="your_username"
export KLARNA_PASSWORD="your_password"
export KLARNA_REGION="eu" # or na, oc
export KLARNA_API_URL="https://api.klarna.com"
export KLARNA_TIMEOUT="30000"
export KLARNA_VERBOSE="false"Create a .env file in your project:
KLARNA_USERNAME=your_username
KLARNA_PASSWORD=your_password
KLARNA_REGION=eu# Set credentials
klarna config set username YOUR_USERNAME
klarna config set password YOUR_PASSWORD
klarna config set region eu
# View current config
klarna config show
# Reset to defaults
klarna config resetConfiguration is stored in ~/.klarna/config.json
- eu: Europe (https://api.klarna.com) - Default
- na: North America (https://api-na.klarna.com)
- oc: Oceania (https://api-oc.klarna.com)
# From JSON string
klarna sessions create --data '{
"order_amount": 10000,
"purchase_country": "US",
"purchase_currency": "USD",
"order_lines": [{
"name": "Red T-Shirt",
"quantity": 1,
"unit_price": 10000,
"total_amount": 10000
}]
}'
# From file
klarna sessions create --file session.json
# With custom region
klarna sessions create --file session.json --region na
# Verbose output
klarna sessions create --file session.json -vklarna sessions get SESSION_ID
# With verbose output
klarna sessions get SESSION_ID -vklarna sessions update SESSION_ID --data '{
"order_amount": 15000,
"order_lines": [...]
}'
# From file
klarna sessions update SESSION_ID --file updated-session.jsonklarna orders create AUTH_TOKEN --data '{
"order_amount": 10000,
"purchase_country": "US",
"purchase_currency": "USD",
"order_lines": [...]
}'
# From file
klarna orders create AUTH_TOKEN --file order.jsonklarna authorizations cancel AUTH_TOKENklarna authorizations customer-token AUTH_TOKEN
# With additional data
klarna authorizations customer-token AUTH_TOKEN --data '{
"purchase_country": "US",
"purchase_currency": "USD"
}'# 1. Create a session
SESSION_RESPONSE=$(klarna sessions create --file session.json)
SESSION_ID=$(echo $SESSION_RESPONSE | jq -r '.session_id')
# 2. Customer completes payment (happens in your frontend)
# You receive an authorization token
# 3. Create order from authorization
klarna orders create $AUTH_TOKEN --file order.json
# 4. If needed, cancel authorization
klarna authorizations cancel $AUTH_TOKEN# Extract specific fields
klarna sessions get $SESSION_ID | jq '.client_token'
# Create session and extract session_id
klarna sessions create --file session.json | jq -r '.session_id' > session_id.txt
# Pipe to file
klarna sessions get $SESSION_ID > session_details.json# Process multiple sessions
for session_id in $(cat session_ids.txt); do
klarna sessions get $session_id >> all_sessions.json
done
# Create orders from multiple auth tokens
while read auth_token; do
klarna orders create $auth_token --file order_template.json
done < auth_tokens.txt#!/bin/bash
if klarna sessions create --file session.json; then
echo "Session created successfully"
exit 0
else
echo "Failed to create session"
exit 1
fi{
"order_amount": 10000,
"purchase_country": "US",
"purchase_currency": "USD",
"order_lines": [
{
"type": "physical",
"name": "Red T-Shirt",
"quantity": 1,
"unit_price": 10000,
"tax_rate": 0,
"total_amount": 10000,
"total_tax_amount": 0
}
],
"billing_address": {
"given_name": "John",
"family_name": "Doe",
"email": "john@example.com",
"street_address": "123 Main St",
"postal_code": "12345",
"city": "New York",
"region": "NY",
"phone": "+15551234567",
"country": "US"
}
}{
"order_amount": 10000,
"purchase_country": "US",
"purchase_currency": "USD",
"order_lines": [
{
"type": "physical",
"name": "Red T-Shirt",
"quantity": 1,
"unit_price": 10000,
"tax_rate": 0,
"total_amount": 10000,
"total_tax_amount": 0
}
],
"merchant_urls": {
"confirmation": "https://example.com/confirmation",
"authorization": "https://example.com/authorization"
}
}0: Success1: Error (API error, validation error, or runtime error)
The CLI provides clear error messages:
- Missing credentials
- Invalid JSON
- API errors with status codes
- Missing required fields
- Network errors
Klarna Payments API uses HTTP Basic Authentication. Provide credentials via:
- Environment variables (recommended for production)
- Config file (
~/.klarna/config.json) - Command-line options (not recommended, credentials visible in shell history)
# Not recommended (visible in shell history)
klarna sessions create --username USER --password PASS --file session.jsonAll commands return JSON responses from the Klarna API or formatted error messages:
{
"session_id": "abc-123-def-456",
"client_token": "token_value",
"order_amount": 10000,
...
}✗ Failed to create session (400)
Constraint violation: Invalid purchase_country
npm testnode bin/klarna.js --helpexport KLARNA_VERBOSE=true
klarna sessions create --file session.jsonError: Authentication credentials not found
Solution: Set KLARNA_USERNAME and KLARNA_PASSWORD environment variables or use klarna config set
Error: Unexpected token in JSON
Solution: Validate your JSON with jq or an online validator
Error: No response from server
Solution: Increase timeout with klarna config set timeout 60000 or check network connectivity
Error: Failed to create session (403)
Solution: Verify you're using the correct region for your account (--region eu/na/oc)
- GitHub Issues: [Report bugs or request features]
- Documentation: See
AGENT.mdfor AI agent integration - OpenClaw Integration: See
OPENCLAW.md
MIT
Contributions welcome! Please read the contributing guidelines before submitting PRs.
Built with Commander.js • Powered by Klarna Payments API V1