How to fetch sales invoices from the Moneybird API using Python 

Moneybird is a comprehensive, intuitive, accounting solution, specifically built for entrepreneurs and small and medium-sized businesses. It allows users to create and deliver invoices, receive and pay them, analyze their financials, provide links that allow clients to make payments, and much more.

While you likely want all kinds of data in Moneybird to be accessible elsewhere, sales invoices likely top your list. Syncing sales invoices with a CRM solution can, for instance, help customer-facing employees stay up-to-date on client payments and step in when necessary. And adding sales invoices—alongside other client data—to a business intelligence (BI) platform can help your analysts uncover trends and actionable insights for your go-to-market teams.

To help your team GET sales invoices form Moneybird, we’ll walk you through the steps of building a Python script that can retrieve this data.  

Authentication

The first step involves configuring authentication.

The Moneybird API uses token-based authentication, which means you need to include a specific header in your requests to the API. The header's format should be <code class="blog_inline-code">Authorization: Bearer {API-KEY}</code>, where <code class="blog_inline-code">{API-KEY}</code> is the API key that’s specific to your account and is encoded in Base-64. It's essential to have this key set up correctly as it’s used to authenticate your requests and ensure they come from an authorized source.

How to GET sales invoices from Moneybird

The code below collects all your sales invoices from the Moneybird API and stores them in the invoices list. The loop will continue fetching invoices until there are no more pages to retrieve, as indicated by the lack of a 'next' link in the response headers. If there's any error with the request, the code will print out the HTTP status code and break the loop. 

Here's the entire Python script:


import requests
import base64
import json

Set your API Key and domain

api_key = "{API-KEY}"
domain = "{DOMAIN}"


Base URL for Moneybird API

base_url = f"https://moneybird.com/api/v2/{domain}/sales_invoices"


Create the header for authentication

auth_header = {
    "Authorization": f"Bearer {api_key}{base64.b64encode(api_key.encode()).decode()}"
}


Initialize variables to hold invoices and the next cursor

invoices = []
next_cursor = None


Fetch invoices from Moneybird API

while True:


# Update the URL with the next cursor if it exists
if next_cursor:
    url = f"{base_url}?page={next_cursor}"
else:
    url = base_url

# Make the GET request to the Moneybird API
response = requests.get(url, headers=auth_header)

# Check for a successful response
if response.status_code == 200:
    # Load the JSON data from the response
    data = json.loads(response.text)

    # Append the invoices to the list
    invoices.extend(data)

    # Check for the next cursor in the Link header
    if 'next' in response.links:
        next_cursor = response.links['next']['url']
    else:
        # If there's no next cursor, we've reached the last page
        break
else:
    print(f"Error: {response.status_code}")
    break

Note: The API key and domain should be replaced with your specific information.

Here’s how an individual item can be returned by this API Endpoint:


{
    "id": "123456",
    "administration_id": 101,
    "contact_id": "654321",
    "contact": {
        "id": "654321",
        "city": "New York",
        "email": "test@test.com",
        "notes": [],
        "phone": "1234567890",
        "country": "USA",
        "version": 1,
        "zipcode": "10001",
        "address1": "123 Test Street",
        "address2": "Apt 1A",
        "lastname": "Doe",
        "sepa_bic": "TESTBIC",
        "attention": "John Doe",
        "email_ubl": false,
        "firstname": "John",
        "sepa_iban": "TESTIBAN",
        "created_at": "2021-01-01T00:00:00Z",
        "tax_number": "123456789",
        "updated_at": "2021-01-02T00:00:00Z",
        "customer_id": "1234",
        "sepa_active": true,
        "bank_account": "1234567890",
        "company_name": "Test Inc.",
        "custom_fields": [],
        "si_identifier": "1234",
        "contact_people": [],
        "delivery_method": "mail",
        "sepa_mandate_id": "1234",
        "credit_card_type": null,
        "tax_number_valid": null,
        "administration_id": 101,
        "sepa_mandate_date": null,
        "credit_card_number": "  ** 1234",
        "sales_invoices_url": "https://example.com/test",
        "sepa_sequence_type": "RCUR",
        "si_identifier_type": null,
        "chamber_of_commerce": "123456",
        "invoice_workflow_id": null,
        "estimate_workflow_id": null,
        "credit_card_reference": "1234",
        "send_invoices_to_email": "invoices@test.com",
        "sepa_iban_account_name": "Test Account",
        "send_estimates_to_email": "estimates@test.com",
        "tax_number_validated_at": null,
        "moneybird_payments_mandate": true,
        "send_invoices_to_attention": "John Doe",
        "send_estimates_to_attention": "John Doe"
    },
    "contact_person_id": "123456",
    "contact_person": {
        "id": "123456",
        "email": "john@test.com",
        "phone": "123456789",
        "version": 1,
        "lastname": "Doe",
        "firstname": "John",
        "created_at": "2022-01-01T00:00:00Z",
        "department": "Sales",
        "updated_at": "2022-01-02T00:00:00Z",
        "administration_id": 101
    },
    "invoice_id": "123456",
    "recurring_sales_invoice_id": null,
    "workflow_id": "123456",
    "document_style_id": "123456",
    "identity_id": "123456",
    "draft_id": null,
    "state": "open",
    "invoice_date": "2022-01-01",
    "due_date": "2022-02-01",
    "payment_conditions": "30 days",
    "payment_reference": "123456",
    "short_payment_reference": "123",
    "reference": "123456",
    "language": "en",
    "currency": "USD",
    "discount": "0",
    "original_sales_invoice_id": null,
    "paused": false,
    "paid_at": null,
    "sent_at": "2022-01-01T00:00:00Z",
    "created_at": "2022-01-01T00:00:00Z",
    "updated_at": "2022-01-02T00:00:00Z",
    "public_view_code": "123456",
    "public_view_code_expires_at": "2023-01-01T00:00:00Z",
    "version": 1,
    "details": [],
    "payments": [],
    "total_paid": "0",
    "total_unpaid": "100",
    "total_unpaid_base": "100",
    "prices_are_incl_tax": true,
    "total_price_excl_tax": "100",
    "total_price_excl_tax_base": "100",
    "total_price_incl_tax": "120",
    "total_price_incl_tax_base": "120",
    "total_discount": "0",
    "marked_dubious_on": null,
    "marked_uncollectible_on": null,
    "reminder_count": 0,
    "next_reminder": "2022-02-15",
    "original_estimate_id": null,
    "url": "https://example.com/invoice/123456",
    "payment_url": "https://example.com/invoice/123456/payment",
    "custom_fields": [],
    "notes": [],
    "attachments": [],
    "events": [],
    "tax_totals": []
}

Related: Everything you need to know to perform API integration testing

Want to connect to more accounting systems?

While collecting sales invoices from Moneybird is, clearly, valuable, you should look to build several other accounting integrations with your product to meet all of your clients’ financial data needs. 

Merge, the leading Unified API platform, helps you do just that by offering an Accounting Unified API. Once you connect to the API, you’ll be able to offer integrations with MoneyBird, Sage Intacct, NetSuite, Freshbooks, and several other applications, and not only sync sales invoices but also other business-critical objects and fields. 

Learn more about Merge and its Accounting Unified API by scheduling a demo with one of our integration experts.