QuickBooks Online API Client

0.9.12 · active · verified Wed Apr 15

python-quickbooks is an actively maintained Python 3 library designed for interacting with the QuickBooks Online API. It provides a convenient object-oriented interface to access and manage QuickBooks data, abstracting away the complexities of the REST API and OAuth 2.0 authentication. The library integrates with `intuit-oauth` for secure authentication. Releases appear on an as-needed basis, with multiple updates throughout the year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the `AuthClient` and `QuickBooks` client using environment variables for credentials. It then shows how to fetch a list of customers. Ensure you have registered your app with Intuit Developer and obtained `CLIENT_ID`, `CLIENT_SECRET`, `REFRESH_TOKEN`, `COMPANY_ID` (Realm ID), and configured a `REDIRECT_URI`.

import os
from intuitlib.client import AuthClient
from quickbooks import QuickBooks
from quickbooks.objects.customer import Customer

# Retrieve credentials from environment variables for security
CLIENT_ID = os.environ.get('QBO_CLIENT_ID', 'YOUR_CLIENT_ID')
CLIENT_SECRET = os.environ.get('QBO_CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
REFRESH_TOKEN = os.environ.get('QBO_REFRESH_TOKEN', 'YOUR_REFRESH_TOKEN')
COMPANY_ID = os.environ.get('QBO_COMPANY_ID', 'YOUR_COMPANY_ID') # Also known as Realm ID
REDIRECT_URI = os.environ.get('QBO_REDIRECT_URI', 'http://localhost:8000/callback')
ENVIRONMENT = os.environ.get('QBO_ENVIRONMENT', 'sandbox') # 'sandbox' or 'production'

# Initialize AuthClient
auth_client = AuthClient(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URI,
    ENVIRONMENT
)

# Initialize QuickBooks client
qb = QuickBooks(
    auth_client=auth_client,
    refresh_token=REFRESH_TOKEN,
    company_id=COMPANY_ID,
    minorversion=75 # Recommended: use the latest supported minor version
)

try:
    # Automatically refresh token if needed (handled by the library)
    # Fetch all customers
    customers = Customer.all(qb=qb, max_results=10) # Limit results for demonstration
    for customer in customers:
        print(f"Customer ID: {customer.Id}, Display Name: {customer.DisplayName}")

    # Example: Create a new customer (uncomment to run)
    # new_customer = Customer()
    # new_customer.DisplayName = "New Test Customer"
    # new_customer.CompanyName = "Test Company, Inc."
    # new_customer.save(qb=qb)
    # print(f"Created new customer: {new_customer.DisplayName} (ID: {new_customer.Id})")

except Exception as e:
    print(f"An error occurred: {e}")
    # Implement robust error handling and token refresh logic in production

view raw JSON →