How to Get External Invoices with the Moneybird API in PHP

by Endgrate Team 2024-06-16 5 min read

Moneybird homepage

Introduction to Moneybird API for External Invoices

Moneybird is a comprehensive online accounting software designed to simplify financial management for businesses. It offers a range of features including invoicing, expense tracking, and financial reporting, making it a popular choice for small to medium-sized enterprises.

Integrating with the Moneybird API allows developers to automate and streamline accounting processes. For example, you can use the Moneybird API to retrieve external invoices, enabling seamless synchronization of invoice data with your internal systems. This can be particularly useful for businesses looking to maintain accurate financial records and improve operational efficiency.

Setting Up Your Moneybird Sandbox Account for API Integration

Before you can start interacting with the Moneybird API, you need to set up a sandbox account. This allows you to test API integrations without affecting live data. Moneybird provides a dedicated sandbox environment for developers to experiment and refine their integrations.

Creating a Moneybird Sandbox Account

To create a sandbox account, follow these steps:

  1. Visit the Moneybird website and register for a user account if you don't already have one.
  2. Once registered, log in to your Moneybird account.
  3. Navigate to the sandbox creation page to set up your sandbox environment.
  4. Follow the on-screen instructions to complete the setup. This will provide you with a sandbox administration where you can test API calls.

Registering Your Application for OAuth Authentication

Moneybird uses OAuth2 for authentication, which requires you to register your application. Here’s how you can do it:

  1. Log in to your Moneybird account and go to the application registration page.
  2. Fill in the necessary details to register your application. You will receive a Client ID and Client Secret upon successful registration.
  3. Ensure you securely store the Client ID and Client Secret, as they are crucial for authenticating API requests.

Obtaining Access Tokens for API Requests

After registering your application, you need to obtain an access token to authenticate API requests:

  1. Use the following cURL command to get a request token and authorization URL:
  2. curl -vv 'https://moneybird.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code'
  3. Redirect the user to the authorization URL provided in the response. The user will authorize your application and receive a code.
  4. Exchange the code for an access token using this command:
  5. curl -vv -X POST -d "client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&code=AUTHORIZATION_CODE&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" https://moneybird.com/oauth/token
  6. Store the access token securely, as it will be used in the Authorization header for API requests.

With your sandbox account and access token ready, you can now proceed to make API calls to retrieve external invoices from Moneybird.

Moneybird authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve External Invoices from Moneybird Using PHP

To interact with the Moneybird API and retrieve external invoices, you need to set up your PHP environment correctly. This section will guide you through the necessary steps, including setting up PHP, installing dependencies, and executing the API call.

Setting Up Your PHP Environment for Moneybird API Integration

Before making API calls, ensure you have the following prerequisites:

  • PHP 7.4 or higher installed on your machine.
  • Composer, the PHP package manager, for managing dependencies.

Once you have PHP and Composer installed, you can proceed to install the required libraries.

Installing Required PHP Libraries for Moneybird API

To make HTTP requests in PHP, you can use the popular Guzzle library. Install it using Composer with the following command:

composer require guzzlehttp/guzzle

This command will add Guzzle to your project, allowing you to make HTTP requests easily.

Example Code to Retrieve External Invoices from Moneybird

With your environment set up, you can now write the PHP code to interact with the Moneybird API and retrieve external invoices. Create a file named get_external_invoices.php and add the following code:


request('GET', 'https://moneybird.com/api/v2/YOUR_ADMINISTRATION_ID/external_sales_invoices.json', [
    'headers' => [
        'Authorization' => 'Bearer YOUR_ACCESS_TOKEN',
        'Content-Type' => 'application/json',
    ],
]);

$data = json_decode($response->getBody(), true);

foreach ($data as $invoice) {
    echo 'Invoice ID: ' . $invoice['id'] . "\n";
    echo 'Reference: ' . $invoice['reference'] . "\n";
    echo 'Total Price: ' . $invoice['total_price_incl_tax'] . "\n\n";
}

?>

Replace YOUR_ADMINISTRATION_ID and YOUR_ACCESS_TOKEN with your actual administration ID and access token obtained during the OAuth setup.

Executing the PHP Script and Verifying the Output

Run the script from the command line using the following command:

php get_external_invoices.php

You should see a list of external invoices printed in the terminal, including their IDs, references, and total prices.

Handling Errors and Verifying API Request Success

To ensure your API requests are successful, check the HTTP status codes returned by the Moneybird API. Here are some common status codes:

  • 200 OK: Request was successful.
  • 401 Unauthorized: Invalid or missing authentication token.
  • 404 Not Found: Resource not found.
  • 429 Too Many Requests: Rate limit exceeded. Retry after the specified time.

For more detailed error handling, refer to the Moneybird API documentation.

Moneybird API call documentation page.

Conclusion and Best Practices for Moneybird API Integration in PHP

Integrating with the Moneybird API using PHP can significantly enhance your business's financial management capabilities by automating invoice retrieval and synchronization. By following the steps outlined in this guide, you can efficiently set up your environment, authenticate using OAuth, and make API calls to retrieve external invoices.

Best Practices for Secure and Efficient Moneybird API Usage

  • Secure Storage of Credentials: Always store your Client ID, Client Secret, and access tokens securely. Consider using environment variables or secure vaults to manage sensitive information.
  • Handle Rate Limits: Be mindful of Moneybird's rate limits, which allow 150 requests every 5 minutes. Implement logic to handle the 429 Too Many Requests status code and retry after the specified time.
  • Data Standardization: Ensure that data retrieved from Moneybird is standardized and transformed as needed to fit your internal systems. This will help maintain consistency across your applications.
  • Error Handling: Implement robust error handling to manage different HTTP status codes and provide meaningful feedback to users or logs for debugging purposes.

Streamline Your Integrations with Endgrate

While integrating with Moneybird can be straightforward, managing multiple integrations can become complex and time-consuming. Endgrate offers a unified API solution that simplifies the integration process across various platforms, including Moneybird. By using Endgrate, you can save time and resources, allowing you to focus on your core product development.

Explore how Endgrate can enhance your integration strategy by visiting Endgrate's website and discover a more efficient way to manage your business integrations.

Read More

Ready to get started?

Book a demo now

Book Demo