Using the Affinity API to Get Companies (with PHP examples)

by Endgrate Team 2024-06-25 4 min read

Affinity homepage

Introduction to Affinity API

Affinity is a powerful relationship intelligence platform designed to help businesses manage and grow their networks. It offers a comprehensive suite of tools to streamline relationship management, track interactions, and enhance collaboration across teams.

Developers may want to integrate with the Affinity API to access and manage company data, enabling them to automate processes and enhance productivity. For example, a developer might use the Affinity API to retrieve a list of companies associated with their organization, allowing for seamless integration of company data into their internal systems.

Setting Up Your Affinity API Test Account

Before you can start integrating with the Affinity API, you'll need to set up a test account. This will allow you to safely experiment with API calls without affecting live data. Affinity provides a straightforward way to generate an API key for authentication purposes.

Creating an Affinity Account

  1. Visit the Affinity website and sign up for an account if you don't already have one.
  2. Once registered, log in to your Affinity account.

Generating an API Key for Affinity

Affinity uses API key-based authentication to secure API requests. Follow these steps to generate your API key:

  1. Navigate to the Settings panel from the left sidebar of the Affinity web app.
  2. Locate the section for API keys and click on Generate API Key.
  3. Copy the generated API key and store it securely. You will need this key to authenticate your API requests.

Understanding Affinity API Authentication

Affinity API requests are authenticated using HTTP Basic Auth. You will use your API key as the password, and no username is required. Here's an example of how to structure your API requests:

curl "https://api.affinity.co/api_endpoint" -u :$APIKEY

Replace $APIKEY with your actual API key. This setup ensures that your requests are secure and authenticated.

Testing Your Affinity API Setup

To verify that your API key is working correctly, you can make a simple API request to fetch some data. For example, you can retrieve a list of companies:

curl "https://api.affinity.co/organizations" -u :$APIKEY

If the request is successful, you should receive a JSON response containing company data. If you encounter any errors, double-check your API key and ensure that your request is correctly formatted.

Affinity authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve Companies Using PHP and Affinity API

To effectively interact with the Affinity API and retrieve company data, you'll need to use PHP to make HTTP requests. This section will guide you through the process of setting up your PHP environment, making API calls, and handling responses.

Setting Up Your PHP Environment for Affinity API Integration

Before making API calls, ensure you have the following prerequisites installed on your machine:

  • PHP 7.4 or higher
  • cURL extension for PHP

To install the cURL extension, you can use the following command:

sudo apt-get install php-curl

Making a GET Request to Fetch Companies from Affinity API

Now that your environment is set up, you can proceed to make a GET request to the Affinity API to retrieve a list of companies. Below is a sample PHP script to achieve this:


<?php
$apiKey = 'Your_API_Key';
$url = 'https://api.affinity.co/organizations';

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

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, ":$apiKey");

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

// Check for errors
if (curl_errno($ch)) {
    echo 'Request Error:' . curl_error($ch);
} else {
    // Decode JSON response
    $data = json_decode($response, true);
    print_r($data);
}

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

Replace Your_API_Key with your actual API key. This script initializes a cURL session, sets the necessary authentication headers, and executes the request to fetch company data.

Handling API Responses and Errors

After executing the API call, it's crucial to handle the response correctly. The Affinity API returns data in JSON format, which you can decode and manipulate as needed. Additionally, be prepared to handle potential errors, such as:

  • 401 Unauthorized: Check if your API key is correct.
  • 404 Not Found: Verify the endpoint URL.
  • 429 Too Many Requests: Implement rate limiting strategies.

For more detailed error handling, refer to the Affinity API documentation on error codes and responses.

Verifying Successful API Requests in Affinity

To ensure your API requests are successful, you can verify the returned data against your Affinity account. Log in to your Affinity dashboard and check the list of companies to confirm that the data matches the API response.

By following these steps, you can seamlessly integrate Affinity's company data into your PHP applications, enhancing your business processes and productivity.

Conclusion and Best Practices for Using Affinity API with PHP

Integrating the Affinity API into your PHP applications can significantly enhance your ability to manage and utilize company data effectively. By following the steps outlined in this guide, you can streamline your processes and improve productivity.

Best Practices for Secure and Efficient API Integration

  • Secure API Key Storage: Always store your API keys securely and avoid hardcoding them directly into your scripts. Consider using environment variables or secure vaults.
  • Implement Rate Limiting: Be mindful of Affinity's rate limits, which are set at 900 requests per user per minute. Implement strategies to handle 429 Too Many Requests errors gracefully.
  • Data Standardization: Ensure that the data retrieved from Affinity is standardized and transformed as needed to fit your internal systems.

Enhance Your Integration Experience with Endgrate

For developers looking to simplify the integration process, consider using Endgrate. With Endgrate, you can save time and resources by outsourcing integrations, allowing you to focus on your core product. Build once for each use case and enjoy an intuitive integration experience for your customers.

Visit Endgrate to learn more about how you can streamline your integration processes and enhance your business operations.

Read More

Ready to get started?

Book a demo now

Book Demo