The majority of production Python web applications rely on several externally hosted application programming interfaces (APIs). APIs are also commonly referred to as third party services or external platforms. Examples include Twilio for messaging and voice services, Stripe for payment processing and Disqus for embedded webpage comments.
There are many articles about proper API design but best practices for integrating APIs is less commonly written about. However, this subject continuously grows in importance because APIs provide critical functionality across many implementation areas.
Hosted API testing services
- Runscope is a service specifically designed for APIs that assists developers with automated testing and traffic inspection.
- Apiary provides a blueprint for creating APIs so they are easier to test and generate clean documentation.
API Integration Resources
- Some developers prefer to use Requests instead of an API’s helper library. In that case check out this tutorial on using requests to access web APIs.
- Product Hunt lists many commonly used commercial and free web APIs to show “there’s an API for everything”.
- There’s a list of all government web APIs at 18F’s API-All-the-X list. The list is updated whenever a new API comes online.
- If you use Requests check out this handy guide on gracefully handling HTTP errors with Python.
- John Sheehan’s “Zen and the Art of API Maintenance” slides are relevant for API integration.
- This post on “API Driven Development” by Randall Degges explains how using APIs in your application cuts down on the amount of code you have to write and maintain so you can launch your application faster.
- Safe Sex with Third Party APIs is a funny high level overview of what you should do to protect your application when relying on third party services.
- Retries in Requests is a nice tutorial for easily re-executing failed HTTP requests with the Requests library.
- My DjangoCon 2013 talk dove into “Making Django Play Nice With Third Party Services.”
- If you’re looking for a fun project that uses two web APIs within a Django application, try out this tutorial to Build your own Pokédex with Django, MMS and PokéAPI.
- vcr.py is a way to capture and replay HTTP requests with mocks. It’s extremely useful for testing API integrations.
- Caching external API requests is a good post on how to potentially limit the number of HTTP calls required when accessing an external web API via the Requests library.
- Working with APIs the Pythonic way does a nice job walking through creating a naive web API client, building on it with better error handling and then explaining how to test it by mocking out the service.
API integration learning checklist
- Pick an API known for top notch documentation. Here’s a list of ten APIs that are a good starting point for beginners.
- Read the API documentation for your chosen API. Figure out a simple use case for how your application could be improved by using that API.
- Before you start writing any code, play around with the API through the commandline with cURL or in the browser with Postman. This exercise will help you get a better understanding of API authentication and the data required for requests and responses.
- Evaluate whether to use a helper library or work with Requests. Helper libraries are usually easier to get started with while Requests gives you more control over the HTTP calls.
- Move your API calls into a task queue so they do not block the HTTP request-response cycle for your web application.