Usage
Installation
Install the bunq Python SDK using pip:
pip install bunq_sdk --upgradeGetting Started
Creating an API Context
Before you can make any API calls, you need to create an API context. This involves registering your API key and device, and creating a session.
from bunq.sdk.context.api_context import ApiContext
from bunq.sdk.context.bunq_context import BunqContext
from bunq import ApiEnvironmentType
# Create an API context for production
api_context = ApiContext.create(
ApiEnvironmentType.PRODUCTION, # SANDBOX for testing
"YOUR_API_KEY",
"My Device Description"
)
# Save the API context to a file for future use
api_context.save("bunq_api_context.conf")
# Load the API context into the SDK
BunqContext.load_api_context(api_context)Note: Initializing an API context is a heavy operation and should only be done once per device.
Saving and Restoring the API Context
After creating an API context, you can save it to a file and restore it in future sessions:
PSD2 Integration
The SDK supports PSD2 (Payment Services Directive 2) integration, allowing you to act as a PSD2 Service Provider:
Note: For sandbox environments, any certificate meeting basic criteria will be accepted. For production, you'll need a valid eIDAS certificate.
Safety Considerations
The file storing your API context (e.g., bunq_api_context.conf) contains sensitive information that provides access to your bunq account. Store this file in a secure location and ensure it's not accessible to unauthorized users.
Core Concepts
API Context
The API context contains your authentication credentials and session information for interacting with the bunq API. It includes:
API key
Installation token
Session token
Server public key
Environment type (Sandbox or Production)
User Context
The user context represents the user you're authenticated as and provides access to account-specific operations:
Monetary Accounts
Monetary accounts are banking accounts within bunq. A user can have multiple monetary accounts, with one designated as the primary account.
Making API Calls
The SDK follows a consistent pattern for API calls. Each endpoint has a corresponding class with methods for supported operations.
Creating Objects
To create a new resource:
Reading Objects
To retrieve a specific resource:
Updating Objects
To update an existing resource:
Deleting Objects
To delete a resource:
Listing Objects
To retrieve a list of resources:
Working with Resources
Payments
Make payments between accounts:
Monetary Accounts
Create and manage monetary accounts:
Cards
Manage debit cards:
Attachments and Avatars
Work with attachments and avatars:
Requests
Create and respond to payment requests:
Sharing
Share accounts with other bunq users:
Generating Share QR Code
Advanced Features
Pagination
The SDK supports pagination for listing resources:
Notification Filters
Set up notification filters (webhooks) to receive updates when certain events occur:
Setting up Account-Specific Notification Filters
You can also set up notification filters for specific monetary accounts:
OAuth
Use OAuth for authentication:
Session Management
Manage API sessions:
Error Handling
The SDK uses exceptions to handle errors:
Examples
Example: Making a Payment
Example: Listing Transactions
Troubleshooting
Common Issues and Solutions
Authentication Failures
Ensure your API key is valid
Check that your API context file is up-to-date
Try refreshing your session with
BunqContext.api_context().ensure_session_active()
Expired Sessions
Sessions automatically expire; use
api_context.ensure_session_active()to refresh if neededThe SDK will usually handle this automatically
Missing Permissions
Ensure your API key has the necessary permissions for the actions you're trying to perform
Resource Not Found
Verify that the resource IDs you're using are correct
Check if the resources still exist (e.g., a monetary account may have been closed)
Rate Limiting
The bunq API has rate limits; implement exponential backoff if you encounter rate limiting
For additional help, refer to the bunq Developer Portal or the bunq forum.
Last updated
Was this helpful?