This post will be short. I wanted to share some of the tools and techniques I employed whilst cutting code to integrate my applications with the Twitter Ads API. This post will outline at a high level the main components you can use from a .NET perspective. It is not a detailed “how to” in terms of the calls you need to make.
You can contact me if you would like more detailed information on integrating the Twitter Ads API with your application. Lets get started.
Consuming the Twitter API is done through REST requests, you could craft your own class using the HttpRequest object in .NET but I found Postman to be very useful. This allows you to construct the requests through an easy GUI and submit them to the Twitter endpoints you wish to consume. You’re going to be using this a lot to quickly test endpoints and help you visualise the requests/responses you’ll need to deal with.
This easy to follow video here will guide you through using Postman.
You will get a streams of JSON returned which takes me onto my next point.
To sift through the nodes of JSON returned from the Twitter API, you’ll want to be able to drill into the relevant nodes that you’re interested in. You can use this library to do this.
POCOS and JSON
You’ll want an easy way to work with the JSON in your (I’m assuming) service oriented/layered application.
I manually handcrafted (control freak) all of my POCOS when building the required objects to support the AdTech features for Social Opinion. You can choose to do this or you can use one of the free online “JSON to C#” converters.
Pulling It Together
We’ve covered the main concepts. This next part is where the bulk of the work is.
The most difficult task you’ll have will be identifying which API endpoints you need to use in relation to the functionality you want to build but don’t worry, the Twitter Ads API is rich and contains documentation and reference guides for the expected inputs/outs for each endpoint.
- I recommend getting out a pen and paper and plotting the various requests you believe you’re going to need. You could model these in a UML Sequence Diagram if you wanted but this is something I’d only do if someone else was responsible for writing the code, OR if I had to share the design with another party to help get them up to speed quickly on the system architecture .
- When you have this mapped out, you then need to identify the expected inputs (parameters) and output JSON from each of these requests. I suggest mapping the output of your JSON requests to the Plain Old Common Objects (POCOS) and encapsulating all of these in a class library.
- You should also encapsulate the orchestration of these calls in “manager” class which exposes one or many public methods to give you something like this:
- In the code extract above, you can see my “manager” class with a public method GetAvailableAccounts. Internally, this method makes all the necessary OAuth calls and the respective REST request to the Twitter Ads API to fetch the JSON that contains the Advertising accounts associated with a given set of OAuth credentials. At this point, you might want to update your application database or perform some other activity.
- The content of the JSON would then be mapped to the POCO you have identified in another layer / class within your application.
Note: It is worth mentioning that your manager class could perform the mapping from JSON to POCO, it’s entirely up to you. I wanted to build my own rich C# API that sits directly on top of the Twitter Ads API that did not depend on a specific set of POCOs.
So there we have it, how to consume the Twitter Ads API when building a .NET application. I arrived at this process whilst building the AdTech features for Social Opinion and thought I’d share some the things that helped.
If you’d like more information on a particular topic then please do get in touch.
Are you a .NET developer working with the Twitter Ads API?
Would you like support or to swap ideas?