Braintree Python SDK
PayPal's Braintree payment gateway Python SDK. Current version: 4.42.0 (Mar 2026). Stable API — no major breaking changes from v3 to v4 for Python. SSL certificate update required: Braintree updated root SSL cert April 2024 — SDKs older than ~4.33 will fail after March 2026. Three-step flow: generate client token server-side → collect payment method nonce client-side → create transaction server-side. Amount as string. submit_for_settlement must be True to actually capture funds.
Common errors
-
ModuleNotFoundError: No module named 'braintree'
cause The Braintree Python SDK is not installed in the current Python environment or the environment where the code is being executed.fixInstall the braintree package using pip: `pip install braintree` -
braintree.exceptions.AuthenticationError
cause Your Braintree API credentials (Merchant ID, Public Key, Private Key) are incorrect, or you are attempting to use credentials for a different environment (e.g., sandbox keys in production or vice-versa).fixDouble-check your Braintree API keys and ensure you are using the correct environment (Sandbox vs. Production) when initializing the Braintree gateway. -
SSLError: SSLCertificateError
cause Braintree updated its root SSL certificate in April 2024. Older Python SDK versions (specifically those older than ~4.33) will fail to connect after March 2026 due to an outdated SSL certificate, or your system's OpenSSL version is too old.fixUpgrade your Braintree Python SDK to the latest version (`pip install --upgrade braintree`). If the issue persists, ensure your system's OpenSSL libraries are up to date. -
AttributeError: 'module' object has no attribute 'Configuration' or AttributeError: partially initialized module 'braintree' has no attribute 'BraintreeGateway' (most likely due to a circular import)
cause You have a Python file named 'braintree.py' in your project's directory, which causes a circular import when you try to import the official 'braintree' SDK.fixRename your local Python file (e.g., to `braintree_utils.py` or `payment_gateway.py`) to avoid naming conflicts with the installed `braintree` package.
Warnings
- breaking SSL certificate update: Braintree updated root SSL cert April 2024. SDK versions older than ~4.33.0 will stop working after March 30, 2026. Update to latest version.
- gotcha Amount must be a string ('100.00'), not float or int. Passing a number raises an error or causes unexpected behavior.
- gotcha Without submit_for_settlement: True, transactions are only authorized — not captured. Funds are not collected. Must explicitly settle.
- gotcha result.is_success being False doesn't always mean total failure — check result.transaction for processor decline (card declined) vs result.errors for validation errors.
- breaking AuthenticationError indicates invalid Braintree API credentials (merchant_id, public_key, private_key) or incorrect environment configuration.
- breaking AuthenticationError: This typically indicates incorrect Braintree API credentials (Merchant ID, Public Key, Private Key) or an issue with their configuration. Ensure your gateway credentials are set up correctly.
Install
-
pip install braintree
Imports
- BraintreeGateway
# Wrong: amount as float result = gateway.transaction.sale({'amount': 100.00, ...}) # Wrong: forgetting submit_for_settlement — authorizes but doesn't capture result = gateway.transaction.sale({ 'amount': '100.00', 'payment_method_nonce': nonce # no options — payment authorized but not captured })import braintree gateway = braintree.BraintreeGateway( braintree.Configuration( environment=braintree.Environment.Sandbox, merchant_id='your_merchant_id', public_key='your_public_key', private_key='your_private_key' ) ) # Generate client token — send to frontend client_token = gateway.client_token.generate() # Create transaction with nonce from frontend result = gateway.transaction.sale({ 'amount': '100.00', # string, not float 'payment_method_nonce': nonce_from_client, 'options': { 'submit_for_settlement': True # capture immediately } }) if result.is_success: print(result.transaction.id) else: for error in result.errors.deep_errors: print(error.message)
Quickstart
# pip install braintree
import braintree
gateway = braintree.BraintreeGateway(
braintree.Configuration(
environment=braintree.Environment.Sandbox,
merchant_id='your_merchant_id',
public_key='your_public_key',
private_key='your_private_key'
)
)
# Step 1: Generate client token for frontend
client_token = gateway.client_token.generate()
# Pass client_token to frontend Braintree Drop-in UI
# Step 2: Receive nonce from frontend, create transaction
result = gateway.transaction.sale({
'amount': '49.99',
'payment_method_nonce': 'nonce-from-frontend',
'options': {'submit_for_settlement': True}
})
if result.is_success:
print('Success:', result.transaction.id)
elif result.transaction:
print('Processor error:', result.transaction.processor_response_code)
else:
for err in result.errors.deep_errors:
print('Validation error:', err.message)