Avalara Tax Python SDK

26.3.2 · active · verified Sat Apr 11

The Avalara Tax Python SDK provides an interface to the Avalara AvaTax REST V2 API for calculating sales tax, managing tax profiles, and handling compliance. It offers functionalities for transaction processing, nexus management, and address validation. The library is actively maintained with frequent releases, often monthly, reflecting updates to the underlying AvaTax API.

Warnings

Install

Imports

Quickstart

Initializes the Avalara client using credentials loaded from environment variables and demonstrates a basic connectivity check with the `ping` API. It then shows how to create a simple sales tax transaction, passing a dictionary payload that maps to the `TransactionModel` structure.

import os
from avalara import AvalaraClient

# --- Configuration (recommended via environment variables) ---
# Replace 'YOUR_...' with actual credentials or ensure environment variables are set
username = os.environ.get('AVALARA_USERNAME', 'YOUR_USERNAME')
password = os.environ.get('AVALARA_PASSWORD', 'YOUR_PASSWORD')
app_name = os.environ.get('AVALARA_APP_NAME', 'MyApp')
app_version = os.environ.get('AVALARA_APP_VERSION', '1.0')
machine_name = os.environ.get('AVALARA_MACHINE_NAME', 'MyMachine')
environment = os.environ.get('AVALARA_ENVIRONMENT', 'sandbox') # 'sandbox' or 'production'

if username == 'YOUR_USERNAME':
    print("Warning: Please set AVALARA_USERNAME and other env vars for a real test.")

client = AvalaraClient(
    username=username,
    password=password,
    app_name=app_name,
    app_version=app_version,
    machine_name=machine_name,
    environment=environment
)

# --- Example: Ping API (check connectivity) ---
try:
    ping_result = client.ping()
    print(f"Ping successful: {ping_result.message}")

    # --- Example: Create a simple tax transaction (using a dictionary payload) ---
    transaction_payload = {
        'lines': [
            {
                'itemCode': 'PC0001',
                'quantity': 1,
                'amount': 100.00
            }
        ],
        'type': 'SalesOrder',
        'companyCode': 'DEFAULT',
        'customerCode': 'ABC',
        'date': '2024-01-01',
        'commit': False,
        'addresses': {
            'singleLocation': {
                'line1': '123 Main St',
                'city': 'Irvine',
                'region': 'CA',
                'postalCode': '92612',
                'country': 'US'
            }
        }
    }

    transaction_result = client.create_transaction(model=transaction_payload)
    print(f"Transaction created successfully. Total tax: {transaction_result.totalTax}")
    for line in transaction_result.lines:
        print(f" - Item {line.itemCode}: Tax {line.tax}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →