Skip to main content

Python SDK for the Asaas payment platform API v3

Project description

Asaas Python SDK

SDK Python completo para a API v3 da plataforma de pagamentos Asaas.

Instalação

pip install asaas-sdk

Ou instale a partir do código fonte:

pip install -e .

Início Rápido

from asaas import Asaas

# Inicializar cliente (sandbox para testes)
client = Asaas(api_key="sua_api_key", sandbox=True)

# Criar cliente
customer = client.customers.create(
    name="João Silva",
    cpf_cnpj="24971563792",
    email="joao@example.com",
)

# Criar cobrança via boleto
payment = client.payments.create(
    customer=customer["id"],
    billing_type="BOLETO",
    value=100.50,
    due_date="2025-12-31",
)

# Obter linha digitável do boleto
boleto = client.payments.get_identification_field(payment["id"])
print(boleto["identificationField"])

# Criar cobrança via PIX
pix_payment = client.payments.create(
    customer=customer["id"],
    billing_type="PIX",
    value=75.00,
    due_date="2025-12-31",
)
qr = client.payments.get_pix_qr_code(pix_payment["id"])
print(qr["payload"])

Configuração

client = Asaas(
    api_key="sua_api_key",
    sandbox=True,          # False para produção
    timeout=30,            # Timeout em segundos
    max_retries=3,         # Tentativas em caso de erro 5xx
    backoff_factor=0.5,    # Fator de backoff exponencial
)

# Ou use como context manager
with Asaas(api_key="sua_api_key", sandbox=True) as client:
    balance = client.finance.get_balance()

Ambientes

Ambiente URL Base
Produção https://api.asaas.com
Sandbox https://sandbox.asaas.com/api

Recursos Disponíveis

Recurso Acesso Descrição
client.customers Clientes CRUD de clientes
client.payments Cobranças Boleto, PIX, Cartão de crédito
client.subscriptions Assinaturas Cobranças recorrentes
client.installments Parcelamentos Parcelamento com cartão
client.pix PIX Chaves, QR codes, transações
client.accounts Subcontas Gestão de subcontas
client.transfers Transferências Transferências entre contas
client.webhooks Webhooks Notificações de eventos
client.invoices Notas Fiscais Emissão de NFS-e
client.finance Financeiro Saldo e estatísticas
client.bill Pagamento de Contas Pagamento de boletos
client.chargebacks Chargebacks Disputas de estorno
client.checkouts Checkouts Links de checkout
client.payment_links Links de Pagamento Criação de links
client.payment_dunnings Negativação Negativação de devedores
client.anticipations Antecipações Antecipação de recebíveis
client.credit_card Cartão de Crédito Tokenização
client.credit_bureau_report Consulta Serasa Consultas de crédito
client.fiscal_info Info Fiscal Configurações fiscais
client.financial_transactions Extrato Extrato financeiro
client.my_account Minha Conta Configurações da conta
client.notifications Notificações Configuração de notificações
client.lean Lean Payments Cobranças com dados resumidos
client.mobile_phone_recharges Recarga Celular Recargas de celular
client.escrow Conta Escrow Garantias
client.wallets Wallets Identificação de carteiras
client.sandbox_utils Sandbox Utilitários de teste

Exemplos de Uso

Clientes

# Criar
customer = client.customers.create(
    name="Maria Santos",
    cpf_cnpj="12345678901",
    email="maria@example.com",
    mobile_phone="11999999999",
)

# Listar com filtros
page = client.customers.list(name="Maria", limit=20)
for c in page.data:
    print(c["name"], c["id"])

# Paginação automática
for c in client.customers.list_all(max_items=500):
    print(c["name"])

# Atualizar
client.customers.update(customer["id"], name="Maria S. Santos")

# Remover e restaurar
client.customers.delete(customer["id"])
client.customers.restore(customer["id"])

Cobranças

# Boleto
payment = client.payments.create(
    customer="cus_xxx",
    billing_type="BOLETO",
    value=199.90,
    due_date="2025-12-31",
    description="Mensalidade",
    discount={"value": 10.00, "dueDateLimitDays": 5, "type": "FIXED"},
    fine={"value": 1.0},
    interest={"value": 2.0},
)

# Cartão de crédito
cc_payment = client.payments.create(
    customer="cus_xxx",
    billing_type="CREDIT_CARD",
    value=99.90,
    due_date="2025-12-31",
    credit_card={
        "holderName": "john doe",
        "number": "5162306219378829",
        "expiryMonth": "05",
        "expiryYear": "2028",
        "ccv": "318",
    },
    credit_card_holder_info={
        "name": "John Doe",
        "email": "john@example.com",
        "cpfCnpj": "24971563792",
        "postalCode": "89223005",
        "phone": "4738010919",
    },
)

# Estorno
client.payments.refund("pay_xxx")
client.payments.refund("pay_xxx", value=50.0)  # Estorno parcial

# Status e info
status = client.payments.get_status("pay_xxx")
qr = client.payments.get_pix_qr_code("pay_xxx")
boleto = client.payments.get_identification_field("pay_xxx")

Assinaturas

subscription = client.subscriptions.create(
    customer="cus_xxx",
    billing_type="CREDIT_CARD",
    value=49.90,
    next_due_date="2025-03-01",
    cycle="MONTHLY",
    description="Plano Premium",
    credit_card_token="token_xxx",
)

# Listar pagamentos da assinatura
payments = client.subscriptions.list_payments(subscription["id"])

# Atualizar valor
client.subscriptions.update(subscription["id"], value=59.90)

# Cancelar
client.subscriptions.delete(subscription["id"])

Saldo e Extrato

balance = client.finance.get_balance()
print(f"Saldo: R$ {balance['balance']:.2f}")

stats = client.finance.get_payment_statistics()
print(f"Estatísticas: {stats}")

# Extrato
page = client.financial_transactions.list(
    start_date="2025-01-01",
    finish_date="2025-01-31",
)
for tx in page.data:
    print(f"{tx['date']} - R${tx['value']} - {tx['description']}")

Webhooks

webhook = client.webhooks.create(
    url="https://seusite.com/webhook",
    email="admin@seusite.com",
    events=["PAYMENT_CONFIRMED", "PAYMENT_OVERDUE", "PAYMENT_REFUNDED"],
)

Paginação

Todos os endpoints de listagem retornam um PaginatedResponse:

page = client.payments.list(limit=10)
print(page.total_count)  # Total de registros
print(page.has_more)     # Há mais páginas?
print(page.data)         # Lista de registros
print(page.offset)       # Offset atual
print(page.limit)        # Limite por página

Para paginar automaticamente, use os métodos list_all():

# Itera por TODOS os registros automaticamente
for payment in client.payments.list_all():
    print(payment["id"])

# Limitar a quantidade total
for payment in client.payments.list_all(max_items=200):
    print(payment["id"])

Tratamento de Erros

from asaas import (
    AsaasError,
    AsaasAPIError,
    AsaasValidationError,
    AsaasAuthenticationError,
    AsaasNotFoundError,
    AsaasRateLimitError,
    AsaasTimeoutError,
    AsaasConnectionError,
)

try:
    client.payments.create(...)
except AsaasValidationError as e:
    # 400 - Erro de validação
    print(f"Validação: {e.message}")
    for error in e.errors:
        print(f"  [{error['code']}] {error['description']}")
except AsaasAuthenticationError:
    # 401 - API key inválida
    print("Verifique sua API key")
except AsaasNotFoundError:
    # 404 - Recurso não encontrado
    print("Registro não encontrado")
except AsaasRateLimitError:
    # 429 - Rate limit excedido
    print("Aguarde antes de tentar novamente")
except AsaasTimeoutError:
    # Timeout de conexão
    print("Timeout na requisição")
except AsaasConnectionError:
    # Erro de conexão
    print("Erro de conexão com a API")
except AsaasAPIError as e:
    # Qualquer outro erro da API
    print(f"Erro {e.status_code}: {e.message}")

Logging

O SDK usa o módulo logging padrão do Python:

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("asaas")
logger.setLevel(logging.DEBUG)

Sandbox

Utilitários disponíveis apenas no ambiente sandbox:

client = Asaas(api_key="sandbox_key", sandbox=True)

# Simular confirmação de pagamento
client.sandbox_utils.confirm_payment("pay_xxx")

# Forçar vencimento de uma cobrança
client.sandbox_utils.force_overdue("pay_xxx")

Desenvolvimento

# Instalar dependências de desenvolvimento
pip install -e ".[dev]"

# Executar testes
pytest

# Executar testes com cobertura
pytest --cov=asaas

# Linting
ruff check src/

# Type checking
mypy src/asaas/

Requisitos

  • Python 3.8+
  • requests >= 2.28.0

Links

Licença

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asaas-1.0.0.tar.gz (35.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asaas-1.0.0-py3-none-any.whl (48.3 kB view details)

Uploaded Python 3

File details

Details for the file asaas-1.0.0.tar.gz.

File metadata

  • Download URL: asaas-1.0.0.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for asaas-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7f283ad2de08736a2c03aa31b826c0f4ce024971b37b0879817cf8b07b4765c7
MD5 4222f82d3c1ba98b10c9570b4aa021c4
BLAKE2b-256 5e4950e640884b8d29a99288825d331732ba13b4a4e18128e4b46953457de68f

See more details on using hashes here.

File details

Details for the file asaas-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: asaas-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 48.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for asaas-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b2aefa5e44f003380e7d35b9cdc0e2964ce82949e7aceffebfe2d3f483c663a
MD5 b147f38e047c16e17a54150ff0bb0487
BLAKE2b-256 a12ab0897e476643205223f1f568e207ff73e80b178fd415124717142136536e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page