Skip to content

Repository files navigation

GoCardless Python client library

A Python client for interacting with the GoCardless API.

pypi-badge

Tested against Python 3.9, 3.10, 3.11 and 3.12.


Install from PyPI:

$ pip install gocardless_pro

Usage

Create a Client instance, providing your access token and the environment you want to use:

importgocardless_protoken=os.environ['ACCESS_TOKEN']
client=gocardless_pro.Client(access_token=token, environment='live')

Access API endpoints using the corresponding methods on the client object:

# Create a new customer. We automatically add idempotency keys to requests to create# resources, stopping duplicates accidentally getting created if something goes wrong# with the API (e.g. networking problems) - see https://developer.gocardless.com/api# -reference/#making-requests-idempotency-keys for detailscustomer=client.customers.create(params={'email': 'jane@example.com'})
# Fetch a payment by its IDpayment=client.payments.get("PA123")
# Loop through a page of payments, printing each payment's amountforpaymentinclient.payments.list().records:
decimal_amount=decimal.Decimal(payment.amount) /100print('Payment for £{0}'.format(decimal_amount))
# Create a mandate PDF in a specific languageclient.mandate_pdfs.create(
params={'links': {'mandate': 'MD00001234XYZ'}},
headers={'Accept-Language': 'fr'}
)

Rate limit response headers can be read:

# Note these values will be None until you make an API request with the clientclient.rate_limit["ratelimit-limit"]
client.rate_limit["ratelimit-remaining"]
client.rate_limit["ratelimit-reset"]

For full documentation, see our API reference.

Handling webhooks

GoCardless supports webhooks, allowing you to receive real-time notifications when things happen in your account, so you can take automatic actions in response, for example:

  • When a customer cancels their mandate with the bank, suspend their club membership
  • When a payment fails due to lack of funds, mark their invoice as unpaid
  • When a customer's subscription generates a new payment, log it in their "past payments" list

The client allows you to validate that a webhook you receive is genuinely from GoCardless, and to parse it into Event objects which are easy to work with:

importgocardless_pro# When you create a webhook endpoint, you can specify a secret. When GoCardless sends# you a webhook, it will sign the body using that secret. Since only you and GoCardless# know the secret, you can check the signature and ensure that the webhook is truly# from GoCardless.webhook_endpoint_secret=os.environ['GOCARDLESS_WEBHOOK_ENDPOINT_SECRET']
# In your webhook handler (e.g. Flask route)@app.route('/webhooks', methods=['POST'])defhandle_webhook():
request_body=request.datasignature_header=request.headers.get('Webhook-Signature')
try:
events=gocardless_pro.Webhook.parse(
request_body,
signature_header,
webhook_endpoint_secret
)
foreventinevents:
print(event.id)
return'', 200exceptgocardless_pro.errors.InvalidSignatureError:
# The webhook doesn't appear to be genuinely from GoCardlessreturn'', 498

Accessing the webhook ID

If you need to access the webhook ID for debugging purposes, you can use parse_with_meta instead:

result=gocardless_pro.Webhook.parse_with_meta(
request_body,
signature_header,
webhook_endpoint_secret
)
events=result.eventswebhook_id=result.webhook_id# e.g. "WB123" - useful for debugging

Note: The webhook ID is intended for debugging and logging purposes only. It should not be used for deduplication - instead, use the event IDs to deduplicate, as each event has a unique ID that remains consistent if the same event is sent multiple times.

For more details on working with webhooks, see our "Getting Started" guide.

Available resources

Balances

# List balancesclient.balances.list(params={...})
# Iterate through all balancesclient.balances.all(params={...})

Bank account details

# Get encrypted bank detailsclient.bank_account_details.get('BA123', params={...})

Bank account holder verifications

# Create a bank account holder verification.client.bank_account_holder_verifications.create(params={...})
# Get a bank account holder verification.client.bank_account_holder_verifications.get('BAHV123', params={...})

Bank authorisations

# Create a Bank Authorisationclient.bank_authorisations.create(params={...})
# Get a Bank Authorisationclient.bank_authorisations.get('BAU123', params={...})

Bank details lookups

# Perform a bank details lookupclient.bank_details_lookups.create(params={...})

Billing requests

# Create a Billing Requestclient.billing_requests.create(params={...})
# Collect customer detailsclient.billing_requests.collect_customer_details('BRQ123', params={...})
# Collect bank account detailsclient.billing_requests.collect_bank_account('BRQ123', params={...})
# Confirm the payer detailsclient.billing_requests.confirm_payer_details('BRQ123', params={...})
# Fulfil a Billing Requestclient.billing_requests.fulfil('BRQ123', params={...})
# Cancel a Billing Requestclient.billing_requests.cancel('BRQ123', params={...})
# List Billing Requestsclient.billing_requests.list(params={...})
# Iterate through all billing_requestsclient.billing_requests.all(params={...})
# Get a single Billing Requestclient.billing_requests.get('BRQ123', params={...})
# Notify the customerclient.billing_requests.notify('BRQ123', params={...})
# Trigger fallbackclient.billing_requests.fallback('BRQ123', params={...})
# Change currencyclient.billing_requests.choose_currency('BRQ123', params={...})
# Select institution for a Billing Requestclient.billing_requests.select_institution('BRQ123', params={...})

Billing request flows

# Create a Billing Request Flowclient.billing_request_flows.create(params={...})
# Initialise a Billing Request Flowclient.billing_request_flows.initialise('BRF123', params={...})

Billing request templates

# List Billing Request Templatesclient.billing_request_templates.list(params={...})
# Iterate through all billing_request_templatesclient.billing_request_templates.all(params={...})
# Get a single Billing Request Templateclient.billing_request_templates.get('BRT123', params={...})
# Create a Billing Request Templateclient.billing_request_templates.create(params={...})
# Update a Billing Request Templateclient.billing_request_templates.update('BRT123', params={...})

Billing request with actions

# Create a Billing Request with Actionsclient.billing_request_with_actions.create_with_actions(params={...})

Blocks

# Create a blockclient.blocks.create(params={...})
# Get a single blockclient.blocks.get('BLC123', params={...})
# List multiple blocksclient.blocks.list(params={...})
# Iterate through all blocksclient.blocks.all(params={...})
# Disable a blockclient.blocks.disable('BLC123', params={...})
# Enable a blockclient.blocks.enable('BLC123', params={...})
# Create blocks by referenceclient.blocks.block_by_ref(params={...})

Creditors

# Create a creditorclient.creditors.create(params={...})
# List creditorsclient.creditors.list(params={...})
# Iterate through all creditorsclient.creditors.all(params={...})
# Get a single creditorclient.creditors.get('CR123', params={...})
# Update a creditorclient.creditors.update('CR123', params={...})

Creditor bank accounts

# Create a creditor bank accountclient.creditor_bank_accounts.create(params={...})
# List creditor bank accountsclient.creditor_bank_accounts.list(params={...})
# Iterate through all creditor_bank_accountsclient.creditor_bank_accounts.all(params={...})
# Get a single creditor bank accountclient.creditor_bank_accounts.get('BA123', params={...})
# Disable a creditor bank accountclient.creditor_bank_accounts.disable('BA123', params={...})

Currency exchange rates

# List exchange ratesclient.currency_exchange_rates.list(params={...})
# Iterate through all currency_exchange_ratesclient.currency_exchange_rates.all(params={...})

Customers

# Create a customerclient.customers.create(params={...})
# List customersclient.customers.list(params={...})
# Iterate through all customersclient.customers.all(params={...})
# Get a single customerclient.customers.get('CU123', params={...})
# Update a customerclient.customers.update('CU123', params={...})
# Remove a customerclient.customers.remove('CU123', params={...})

Customer bank accounts

# Create a customer bank accountclient.customer_bank_accounts.create(params={...})
# List customer bank accountsclient.customer_bank_accounts.list(params={...})
# Iterate through all customer_bank_accountsclient.customer_bank_accounts.all(params={...})
# Get a single customer bank accountclient.customer_bank_accounts.get('BA123', params={...})
# Update a customer bank accountclient.customer_bank_accounts.update('BA123', params={...})
# Disable a customer bank accountclient.customer_bank_accounts.disable('BA123', params={...})

Customer notifications

# Handle a notificationclient.customer_notifications.handle('EV1D18JEXAMPLE', params={...})

Events

# List eventsclient.events.list(params={...})
# Iterate through all eventsclient.events.all(params={...})
# Get a single eventclient.events.get('EV123', params={...})

Exports

# Get a single exportclient.exports.get('EX123', params={...})
# List exportsclient.exports.list(params={...})
# Iterate through all exportsclient.exports.all(params={...})

Funds availabilities

# Funds availabilityclient.funds_availabilities.check('MD123', params={...})

Instalment schedules

# Create (with dates)client.instalment_schedules.create_with_dates(params={...})
# Create (with schedule)client.instalment_schedules.create_with_schedule(params={...})
# List instalment schedulesclient.instalment_schedules.list(params={...})
# Iterate through all instalment_schedulesclient.instalment_schedules.all(params={...})
# Get a single instalment scheduleclient.instalment_schedules.get('IS123', params={...})
# Update an instalment scheduleclient.instalment_schedules.update('IS123', params={...})
# Cancel an instalment scheduleclient.instalment_schedules.cancel('IS123', params={...})

Institutions

# List Institutionsclient.institutions.list(params={...})
# Iterate through all institutionsclient.institutions.all(params={...})
# List institutions for Billing Requestclient.institutions.list_for_billing_request('BRQ123', params={...})

Logos

# Create a logo associated with a creditorclient.logos.create_for_creditor(params={...})

Mandates

# Create a mandateclient.mandates.create(params={...})
# List mandatesclient.mandates.list(params={...})
# Iterate through all mandatesclient.mandates.all(params={...})
# Get a single mandateclient.mandates.get('MD123', params={...})
# Update a mandateclient.mandates.update('MD123', params={...})
# Cancel a mandateclient.mandates.cancel('MD123', params={...})
# Reinstate a mandateclient.mandates.reinstate('MD123', params={...})

Mandate imports

# Create a new mandate importclient.mandate_imports.create(params={...})
# Get a mandate importclient.mandate_imports.get('IM123', params={...})
# Submit a mandate importclient.mandate_imports.submit('IM123', params={...})
# Cancel a mandate importclient.mandate_imports.cancel('IM123', params={...})

Mandate import entries

# Add a mandate import entryclient.mandate_import_entries.create(params={...})
# List all mandate import entriesclient.mandate_import_entries.list(params={...})
# Iterate through all mandate_import_entriesclient.mandate_import_entries.all(params={...})

Mandate pdfs

# Create a mandate PDFclient.mandate_pdfs.create(params={...})

Negative balance limits

# List negative balance limitsclient.negative_balance_limits.list(params={...})
# Iterate through all negative_balance_limitsclient.negative_balance_limits.all(params={...})

Outbound payments

# Create an outbound paymentclient.outbound_payments.create(params={...})
# Create a withdrawal outbound paymentclient.outbound_payments.withdraw(params={...})
# Cancel an outbound paymentclient.outbound_payments.cancel('OUT123', params={...})
# Approve an outbound paymentclient.outbound_payments.approve('OUT123', params={...})
# Get an outbound paymentclient.outbound_payments.get('OUT123', params={...})
# List outbound paymentsclient.outbound_payments.list(params={...})
# Iterate through all outbound_paymentsclient.outbound_payments.all(params={...})
# Update an outbound paymentclient.outbound_payments.update('OUT123', params={...})
# Outbound payment statisticsclient.outbound_payments.stats(params={...})

Outbound payment imports

# Create an outbound payment importclient.outbound_payment_imports.create(params={...})
# Get an outbound payment importclient.outbound_payment_imports.get('IM123', params={...})
# List outbound payment importsclient.outbound_payment_imports.list(params={...})
# Iterate through all outbound_payment_importsclient.outbound_payment_imports.all(params={...})

Outbound payment import entries

# List outbound payment import entriesclient.outbound_payment_import_entries.list(params={...})
# Iterate through all outbound_payment_import_entriesclient.outbound_payment_import_entries.all(params={...})

Payer authorisations

# Get a single Payer Authorisationclient.payer_authorisations.get('PA123', params={...})
# Create a Payer Authorisationclient.payer_authorisations.create(params={...})
# Update a Payer Authorisationclient.payer_authorisations.update('PA123', params={...})
# Submit a Payer Authorisationclient.payer_authorisations.submit('PA123', params={...})
# Confirm a Payer Authorisationclient.payer_authorisations.confirm('PA123', params={...})

Payer themes

# Create a payer theme associated with a creditorclient.payer_themes.create_for_creditor(params={...})

Payments

# Create a paymentclient.payments.create(params={...})
# List paymentsclient.payments.list(params={...})
# Iterate through all paymentsclient.payments.all(params={...})
# Get a single paymentclient.payments.get('PM123', params={...})
# Update a paymentclient.payments.update('PM123', params={...})
# Cancel a paymentclient.payments.cancel('PM123', params={...})
# Retry a paymentclient.payments.retry('PM123', params={...})

Payment accounts

# Get a single payment account detailsclient.payment_accounts.get('BA123', params={...})
# List payment accountsclient.payment_accounts.list(params={...})
# Iterate through all payment_accountsclient.payment_accounts.all(params={...})

Payment account transactions

# Get a single payment account transactionclient.payment_account_transactions.get('BA123', params={...})
# List payment account transactionsclient.payment_account_transactions.list('BA123', params={...})
# Iterate through all payment_account_transactionsclient.payment_account_transactions.all(params={...})

Payouts

# List payoutsclient.payouts.list(params={...})
# Iterate through all payoutsclient.payouts.all(params={...})
# Get a single payoutclient.payouts.get('PO123', params={...})
# Update a payoutclient.payouts.update('PO123', params={...})

Payout items

# Get all payout items in a single payoutclient.payout_items.list(params={...})
# Iterate through all payout_itemsclient.payout_items.all(params={...})

Redirect flows

# Create a redirect flowclient.redirect_flows.create(params={...})
# Get a single redirect flowclient.redirect_flows.get('RE123456', params={...})
# Complete a redirect flowclient.redirect_flows.complete('RE123456', params={...})

Refunds

# Create a refundclient.refunds.create(params={...})
# List refundsclient.refunds.list(params={...})
# Iterate through all refundsclient.refunds.all(params={...})
# Get a single refundclient.refunds.get('RF123', params={...})
# Update a refundclient.refunds.update('RF123', params={...})

Scenario simulators

# Simulate a scenarioclient.scenario_simulators.run('payment_failed', params={...})

Schemeentifiers

# Create a scheme identifierclient.scheme_identifiers.create(params={...})
# List scheme identifiersclient.scheme_identifiers.list(params={...})
# Iterate through all scheme_identifiersclient.scheme_identifiers.all(params={...})
# Get a single scheme identifierclient.scheme_identifiers.get('SU123', params={...})

Subscriptions

# Create a subscriptionclient.subscriptions.create(params={...})
# List subscriptionsclient.subscriptions.list(params={...})
# Iterate through all subscriptionsclient.subscriptions.all(params={...})
# Get a single subscriptionclient.subscriptions.get('SB123', params={...})
# Update a subscriptionclient.subscriptions.update('SB123', params={...})
# Pause a subscriptionclient.subscriptions.pause('SB123', params={...})
# Resume a subscriptionclient.subscriptions.resume('SB123', params={...})
# Cancel a subscriptionclient.subscriptions.cancel('SB123', params={...})

Tax rates

# List tax ratesclient.tax_rates.list(params={...})
# Iterate through all tax_ratesclient.tax_rates.all(params={...})
# Get a single tax rateclient.tax_rates.get('GB_VAT_1', params={...})

Transferred mandates

# Get updated customer bank detailsclient.transferred_mandates.transferred_mandates('MD123', params={...})

Verification details

# Create a verification detailclient.verification_details.create(params={...})
# List verification detailsclient.verification_details.list(params={...})
# Iterate through all verification_detailsclient.verification_details.all(params={...})

Webhooks

# List webhooksclient.webhooks.list(params={...})
# Iterate through all webhooksclient.webhooks.all(params={...})
# Get a single webhookclient.webhooks.get('WB123', params={...})
# Retry a webhookclient.webhooks.retry('WB123', params={...})

Running tests

First, install the development dependencies:

$ pip install -r requirements-dev.txt

To run the test suite against the current Python version, run pytest.

To run the test suite against multiple Python versions, run tox.

If you don't have all versions of Python installed, you can run the tests in a Docker container by running make.

About

GoCardless Python Client

Resources

Security policy

Stars

42 stars

Watchers

89 watching

Forks

Releases

Packages

Used by

Contributors

Languages