Using the Moneybird API to Get Contacts (with PHP examples)

by Endgrate Team 2024-08-09 5 min read

Moneybird homepage

Introduction to Moneybird API Integration

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 Moneybird's API allows developers to automate and streamline various accounting processes. For example, you can use the Moneybird API to retrieve contact information, enabling seamless synchronization of customer data between your application and Moneybird. This can be particularly useful for businesses looking to maintain up-to-date records across multiple platforms.

Setting Up Your Moneybird Sandbox Account for API Integration

Before you can start using the Moneybird API, you'll need to set up a sandbox account. This allows you to test your integration without affecting your live data. Moneybird provides a sandbox environment that offers full access to all features, ensuring you can thoroughly test your API interactions.

Creating a Moneybird Sandbox Account

  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 create a sandbox administration.
  4. Follow the instructions to set up your sandbox environment. This will provide you with a separate administration ID for testing purposes.

Registering Your Application for OAuth Authentication

Moneybird uses OAuth2 for authentication, which requires you to register your application. This process will provide you with a Client ID and Client Secret necessary for API access.

  1. Log in to your Moneybird account and go to the application registration page.
  2. Fill out the required information to register your application.
  3. Choose to create an external OAuth application to receive your Client ID and Client Secret.
  4. Ensure you securely store these credentials as they are essential for authenticating API requests.

Obtaining Access Tokens for API Requests

With your application registered, you can now obtain an access token to interact with the Moneybird API.

  1. Use the following cURL command to initiate the OAuth authorization process:
  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 your user to the URL provided in the response to authorize your application.
  4. Once authorized, exchange the authorization code for an access token using this cURL 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 for use in API requests.

With your sandbox account and OAuth credentials set up, you're ready to start making API calls to Moneybird. This setup ensures you can test your integration thoroughly without impacting your live data.

Moneybird authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve Contacts from Moneybird Using PHP

Now that you have your Moneybird sandbox account and OAuth credentials set up, it's time to dive into making API calls to retrieve contacts using PHP. This section will guide you through the necessary steps, including setting up your PHP environment, writing the code to interact with the Moneybird API, and handling potential errors.

Setting Up Your PHP Environment for Moneybird API Integration

Before making API calls, ensure your PHP environment is properly configured. You'll need PHP 7.4 or later and the cURL extension enabled, as it will be used to make HTTP requests to the Moneybird API.

  1. Verify your PHP version by running the following command in your terminal:
  2. php -v
  3. Ensure the cURL extension is enabled by checking your php.ini file or running:
  4. php -m | grep curl

Writing PHP Code to Retrieve Contacts from Moneybird

With your environment ready, you can now write the PHP code to retrieve contacts from Moneybird. The following example demonstrates how to make a GET request to the Moneybird API to fetch contact information.


<?php
// Set the API endpoint and access token
$endpoint = 'https://moneybird.com/api/v2/YOUR_ADMINISTRATION_ID/contacts.json';
$accessToken = 'YOUR_ACCESS_TOKEN';

// Initialize cURL session
$ch = curl_init($endpoint);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accessToken
]);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // Parse and display the response
    $contacts = json_decode($response, true);
    foreach ($contacts as $contact) {
        echo 'Contact ID: ' . $contact['id'] . '<br>';
        echo 'Company Name: ' . $contact['company_name'] . '<br>';
        echo 'Email: ' . $contact['email'] . '<br><br>';
    }
}

// Close cURL session
curl_close($ch);
?>

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

Verifying the Success of Your Moneybird API Request

After running the PHP script, you should see a list of contacts displayed. To verify the request's success, check your Moneybird sandbox account to ensure the contacts match the data retrieved by the API call.

Handling Errors and Understanding Moneybird API Response Codes

It's crucial to handle potential errors when making API calls. The Moneybird API provides various HTTP status codes to indicate the outcome of your requests:

  • 200 OK: The request was successful.
  • 401 Unauthorized: Invalid or missing authentication credentials.
  • 404 Not Found: The requested resource could not be found.
  • 429 Too Many Requests: Rate limit exceeded. The Moneybird API allows 150 requests every 5 minutes.

For more detailed information on error codes, refer to the Moneybird API documentation.

Moneybird API call documentation page.

Conclusion and Best Practices for Moneybird API Integration

Integrating with the Moneybird API using PHP can significantly enhance your application's ability to manage financial data efficiently. By automating the retrieval and synchronization of contact information, you can ensure that your records are always up-to-date and accurate.

Best Practices for Secure and Efficient Moneybird API Usage

  • Securely Store Credentials: Always store your Client ID, Client Secret, and access tokens securely. Consider using environment variables or secure vaults to protect sensitive information.
  • Handle Rate Limiting: The Moneybird API allows 150 requests every 5 minutes. Implement logic to handle 429 status codes by pausing requests and retrying after the specified time.
  • Data Standardization: Ensure that data retrieved from Moneybird is standardized and transformed as needed to fit your application's data model.
  • Error Handling: Implement robust error handling to manage different HTTP status codes and provide meaningful feedback to users or logs for debugging.

Streamline Your Integrations with Endgrate

While integrating with Moneybird can be straightforward, managing multiple integrations can become complex. 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 the benefits of a streamlined, efficient integration experience for your business.

Read More

Ready to get started?

Book a demo now

Book Demo