Code Examples

Here are some examples of how you can query the ALF API using some common programming languages:


This code should work in both Python 2 and Python 3.

We recommend you use the requests library to query the API. It is available for both Python 2 and Python 3 and greatly simplifies the code needed to make HTTP requests.

You will need to import the requests module and set your Username and API key. Example code would look like this:

import requests

base_url = "https://api.alfinsight.com/odata/"
key_auth = ("{my username}", "{my api key}")

odata_query = "{my API query}"

url = requests.compat.urljoin(base_url, odata_query)
response = requests.get(url, auth=key_auth)

data = response.json()
print(data)
        

Now you just need to build the URL to query the data you need. Detailed explanations of all the available functions and operators can be found at the Official OData Documentation site.

Some examples:


Fetch the Top 50 highest spending Brands:
odata_query = "Brands?$orderby=TotalBrandSpend desc&$top=50"

Fetch Contacts working for Companies with more than 100,000 employees:
odata_query = "Contacts?$filter=Company/Employees gt 100000"

Fetch Companies with Rolling Digital Spend over £1,000,000, including the Rolling Spend Data:
odata_query = "Companies?$filter=Spend/any(s: s/DigitalSpend gt 1000000 and s/SpendType eq 'Rolling')&$expand=Spend($filter=SpendType eq 'Rolling')"

Fetch Companies updated after 1st October 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

odata_query = "Companies?$filter=DateUpdated gt 2018-10-01T00:00:00Z"

Fetch Trade Press for Tesco updated after 1st September 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

odata_query = "Companies(873)/TradePress?$filter=DateUpdated gt 2018-09-01T00:00:00Z"

Fetching result sets larger than 100

Our API returns a maximum of 100 profiles per request. If the result set of your query is larger than 100, the data returned will include the property @odata.nextLink which indicates the URL to call for the next page of results. Once the result set is exhausted, there will be no @odata.nextLink property.


We recommend you use the NuGet package RestSharp rather than the standard .NET library as this reduces the amount of code needed to make API requests.

An example class that will make API requests to the ALF API: (remember to set username and key to the appropriate values).

using RestSharp;
using RestSharp.Authenticators;

class Program
{
    static void Main(string[] args)
    {
        var username = "{my username}";
        var key = "{my API key}";
        var odataQuery = "{my API query}";

        var client = new RestClient("https://api.alfinsight.com/odata");
        client.Authenticator = new HttpBasicAuthenticator(username, key);

        var request = new RestRequest(odataQuery);
        var response = client.Execute(request).Content;

        System.Diagnostics.Debug.WriteLine(response);
    }
}
        

Now you just need to build the URL to query the data you need. Detailed explanations of all the available functions and operators can be found at the Official OData Documentation site.

Some examples:


Fetch the Top 50 highest spending Brands:
var odataQuery = "Brands?$orderby=TotalBrandSpend desc&$top=50";

Fetch Contacts working for Companies with more than 100,000 employees:
var odataQuery = "Contacts?$filter=Company/Employees gt 100000";

Fetch Companies with Rolling Digital Spend over £1,000,000, including the Rolling Spend Data:
var odataQuery = "Companies?$filter=Spend/any(s: s/DigitalSpend gt 1000000 and s/SpendType eq 'Rolling')&$expand=Spend($filter=SpendType eq 'Rolling')";

Fetch Companies updated after 1st October 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

var odataQuery = "Companies?$filter=DateUpdated gt 2018-10-01T00:00:00Z";

Fetch Trade Press for Tesco updated after 1st September 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

var odataQuery = "Companies(873)/TradePress?$filter=DateUpdated gt 2018-09-01T00:00:00Z";

Fetching result sets larger than 100

Our API returns a maximum of 100 profiles per request. If the result set of your query is larger than 100, the data returned will include the property @odata.nextLink which indicates the URL to call for the next page of results. Once the result set is exhausted, there will be no @odata.nextLink property.

An example class that will make API requests to the ALF API: (remember to set username and key to the appropriate values).

This code was tested on Java 8.

import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import javax.xml.bind.DatatypeConverter;

class AlfApiClient
{
    public static void main(String[] args) throws MalformedURLException, IOException
    {
        String username = "{my username}";
        String key = "{my API key}";
        String baseUrl = "https://api.alfinsight.com/odata";
        
        String odataQuery = "{my API query}";
        
        URL url = new URL(baseUrl + "/" + odataQuery);
        URLConnection conn = url.openConnection();
        String userkey = username + ":" + key;
        String basicAuth = "Basic " + DatatypeConverter.printBase64Binary(userkey.getBytes());
        conn.setRequestProperty ("Authorization", basicAuth);
        InputStream stream = conn.getInputStream();
        
        Scanner scanner = new Scanner(stream).useDelimiter("\\A");
        String data = scanner.hasNext() ? scanner.next() : "";
        
        System.out.println(data);
    }
}
        

Now you just need to build the URL to query the data you need. Detailed explanations of all the available functions and operators can be found at the Official OData Documentation site.

Some examples:


Fetch the Top 50 highest spending Brands:
var odataQuery = "Brands?$orderby=TotalBrandSpend desc&$top=50";

Fetch Contacts working for Companies with more than 100,000 employees:
var odataQuery = "Contacts?$filter=Company/Employees gt 100000";

Fetch Companies with Rolling Digital Spend over £1,000,000, including the Rolling Spend Data:
var odataQuery = "Companies?$filter=Spend/any(s: s/DigitalSpend gt 1000000 and s/SpendType eq 'Rolling')&$expand=Spend($filter=SpendType eq 'Rolling')";

Fetch Companies updated after 1st October 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

var odataQuery = "Companies?$filter=DateUpdated gt 2018-10-01T00:00:00Z";

Fetch Trade Press for Tesco updated after 1st September 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

var odataQuery = "Companies(873)/TradePress?$filter=DateUpdated gt 2018-09-01T00:00:00Z";

Fetching result sets larger than 100

Our API returns a maximum of 100 profiles per request. If the result set of your query is larger than 100, the data returned will include the property @odata.nextLink which indicates the URL to call for the next page of results. Once the result set is exhausted, there will be no @odata.nextLink property.


We recommend you use the Httpful library to make API requests. After downloading Httpful you may need to update the path in the include statement. The below example will work if Httpful.phar is in the same folder as your PHP code.

<?php
include('httpful.phar');

$base_url = 'https://api.alfinsight.com/odata';
$username = '{my username}';
$key = '{my API key}';

$odata_query = '{my API query}';
$url = $base_url . '/' . $odata_query;

$response = \Httpful\Request::get($url)
    ->authenticateWith($username, $key)
    ->send();

echo $response;
        

Now you just need to build the URL to query the data you need. Detailed explanations of all the available functions and operators can be found at the Official OData Documentation site.

Some examples:


Fetch the Top 50 highest spending Brands:
$odata_query = "Brands?$orderby=TotalBrandSpend desc&$top=50";

Fetch Contacts working for Companies with more than 100,000 employees:
$odata_query = "Contacts?$filter=Company/Employees gt 100000";

Fetch Companies with Rolling Digital Spend over £1,000,000, including the Rolling Spend Data:
$odata_query = "Companies?$filter=Spend/any(s: s/DigitalSpend gt 1000000 and s/SpendType eq 'Rolling')&$expand=Spend($filter=SpendType eq 'Rolling')";

Fetch Companies updated after 1st October 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

$odata_query = "Companies?$filter=DateUpdated gt 2018-10-01T00:00:00Z";

Fetch Trade Press for Tesco updated after 1st September 2018:

Please note the date format is very strict - it has to be in the format YYYY-mm-ddTHH:MM:SSZ

$odata_query = "Companies(873)/TradePress?$filter=DateUpdated gt 2018-09-01T00:00:00Z";

Fetching result sets larger than 100

Our API returns a maximum of 100 profiles per request. If the result set of your query is larger than 100, the data returned will include the property @odata.nextLink which indicates the URL to call for the next page of results. Once the result set is exhausted, there will be no @odata.nextLink property.