MGID API Guide - Connecting to the Advertiser API and doing the Client Authentication
While the MGID Advertiser API should help advertisers generate great performance, its interface is a bit messy. Therefore, it’s a good idea to start exploring it shortly after you start your first campaigns. This guide will show you how to connect and do the authentication for the MGID API.
We recently started working more and more with MGID. While performance is generally looking solid after some optimization, I still find the interface a bit confusing, both for reporting and campaign management. So we started looking into the MGID API for advertisers, and as we’ve already produced guides on how to get started with APIs for other native ad platforms, we decided to produce one for MGID as well.
The previous guides to getting started with APIs that we have produced are:
- Taboola API Guide - Getting Started
- Python Guide to Outbrain authentication
- Yahoo Gemini Oauth Authentication
In this guide we’re assuming that you already have an MGID account and some kind of Python development environment installed, we’re using Jupyter Notebook. If you don’t have it set up, you can always follow along in Google’s Colab.
Crendentials and Access to the MGID API
A very nice feature of MGID is that if you have an account, you already have acess to the API. So no need to apply for access like you do for Outbrain, Taboola and Verizon Media Native.
So the only credentials you need to keep track off are just your regular username (email address) and password.
The MGID API Authentication Flow
In order to pull any data out of the MGID platform, or make any campaign changes you need a token
. This is acquired by making a login
call to the API using your username and password credentials. The token will expire after a while so although you can reuse it, make sure that your scripts are ready to request a new token in case the old one is not valid anymore.
We’ll start this off by importing the relevant packages.
from requests import get, post
import json
Then define your credentials and the base url that we will make API calls towards.
url = "http://api.mgid.com/v1/"
user = "exmaple@gmail.com"
password = "Pasword123"
And then the specific login call
response = post(url + "auth/token", data={"email": username, "password": password})
Your code and the response should now look something like this:
The Account id that we receive here idAuth
will also be needed for future API calls to define which accounts you should receive data for or make changes to.
token = response.json()['token']
idAuth = response.json()['idAuth']
Use the code above to save the Token
and the account id (idAuth
) then let’s move on.
Master Advertising On MGID
Register for the ultimate native advertising course and get exclusive insights into building successful MGID campaigns.
Using the Token to make a MGID API Call
So let’s test using the freshly acquired token to actually collect some data from the MGID API.
We’ll test to get all the campaigns associated with your account and their settings. If you have something else in mind that you want to experiment with, here’s the link to the MGID API Documentation.
The resource in the API that we will make a request to is goodhits/clients/{AccountID}/campaigns?
and should be used with a get
request. So the call looks like this:
response = get(url + f"goodhits/clients/{idAuth}/campaigns?", params={"token": token})
So we’re using the account id saved in idAuth
and adding the token as a parameter in the request.
You should now have received a response that contains all of your campaigns and their settings. In order to see only the campaign ID and the campaign names use the following:
for campaign_id, campaign_data in response.json().items():
print(campaign_id, campaign_data['name'])
The results should look similar to these:
What’s Next? Using the MGID API
Now this is more or less all it takes to get started with the MGID API for Advertisers. What to do next with it is completely up to you. We have used to pull out daily reporting data, get updates when our ads have been audited and to block widgets that are performing poorly. But pretty much any workflow that you have for MGID could be done (and probably more effectively) using the API.
A good starting point is the official MGID API documentation which you can find here. There you can see the endpoints for all API calls and what data is needed in the request for MGID to be able to execute/serve them.
If you are looking for better ways of optimizing and running your Native advertising campaigns. Check out our reporting and optimization platform. Or looking to get started with Native Advertising or improving your performance, get in touch.