Accessing Newsletter signups using API

Roman Gomolko Updated by Roman Gomolko

Authorization

To authenticate, you must obtain the AudienceProject API key (client_id and client_secret) and a UserReport account ID.

You can generate your client_id and a client_secret via. the admin.audienceproject.com portal (API keys tab):

Note that you must be an admin to see this tab. Please contact support if you don't see this tab.
  1. When you have your credentials ready you can exchange them to an access token.
    You can use the AudienceProject OAuth server for this. The following example shows how an access token should be received. The authorization header string is Basic Base64Encode(client_id:client_secret).
curl --location --request POST 'https://api.audienceproject.com/oauth/v1/token' \
--header 'Accept: application/json' \
--header 'Authorization: Basic bXlfY2xpZW50X2lkOm15X2NsaWVudF9zZWNyZXQ==' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials'

Each time you need to access API, you must exchange long-living API keys for a short-living API token using the OAuth 2.0 client_credentials flow. The short leaving token expires in 30 minutes.

Send the obtained token as a Bearer authentication token with each request to an API.

ACCESS TOKEN RELATED ERROR RESPONSE

If an access token is missing, malformed, or invalid, you will receive a 401 Unauthorized response code and the following JSON response:

{
"errorType": "AuthenticationException",
"errorMessage": "The application is not authorized to access this resource."
}

If you try to get resources you don't have access tom you will receive a 403 Forbidden response code.

The JavaScript code for doing this is:

const oauthEndpoint = 'https://api.audienceproject.com/oauth/v1/token';
const client_id = 'YOUR_CLIENT_ID';
const client_secret = 'YOU_CLIENT_SECRET';
const auth = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(`${client_id}:${client_secret}`));

const { body } = await fetch(oauthEndpoint, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${auth}`
},
body: 'grant_type=client_credentials',
}).json();

if (body) {
console.log("Token retrieved", body.toke);
} else {
console.error("Unable to obtain token");
}

Access newsletter signups

To get Emails that signed up to receive Newsletter signups, you need to send a POST request to https://api.userreport.com/report/v1/accounts/{accountId}/report/querywith the following JSON body

{
"filters": [{
"dimension": "resultset_email.email",
"operator": "set"
}],
"timeDimensions": [{
"dimension": "resultset_email.conducted",
"dateRange": ["2008-08-01", "2023-05-16"]
}],
"dimensions": [
"resultset_email.age",
"resultset_email.email",
"resultset_email.country",
"resultset_email.postcode",
"resultset_email.gender",
"resultset_email.media_id",
"resultset_email.conducted",
"resultset_email.nps"
],
"order": [{
"id": "resultset_email.conducted",
"desc": true
}],
"limit": 10000
}

Pay attention to the dateRange parameter - you can specify a timeframe for export.

You will receive a JSON with the following structure in response

{
"data": [{
"resultset_email.age": 74,
"resultset_email.email": "...",
"resultset_email.country": "DK",
"resultset_email.postcode": "8920",
"resultset_email.gender": "Female",
"resultset_email.media_id": "267d9939-45fe-4386-b2b7-c3f2f37ea13d",
"resultset_email.conducted": "2023-05-19T08:53:01.283Z",
"resultset_email.nps": 10
}, {
"resultset_email.age": 56,
"resultset_email.email": "...",
"resultset_email.country": "DK",
"resultset_email.postcode": "6200",
"resultset_email.gender": "Male",
"resultset_email.media_id": "c22400df-5e1e-4baa-93ef-43472aa11851",
"resultset_email.conducted": "2023-05-19T07:47:43.649Z",
"resultset_email.nps": 8
}]
}

How did we do?

Contact