3 Tools for Easy Interaction with Web APIs using Python

Felix Vemmer
Geek Culture
Published in
4 min readOct 6, 2021

Originally posted on: https://www.pythoneur.com/easy-interaction-with-web-apis/

Interacting with application programming interfaces (APIs) does not have to be hard. In this article I am sharing my top three tools and hacks to get quickly working with APIs and make your life easy.

Curl to Requests: Generating API requests with a few clicks

Let’s say we would like to monitor and record Etherum Gas fees on ETH Gas Station. With the right tools, you can create an API request in a couple of seconds.

1. Go to the Website, Inspect the request in the Network Tab and copy as cCurl:

2. Next, convert the data from cCurl to Requests here to get the following output:

3. Modify if needed any headers or payload and test your request:

Awesome Data Models with pydantic

Getting all the JSON responses from APIs validated and converted into pythonic objects becomes super easy with Pydantic.

Data validation and settings management using Python type hinting. Fast and extensible, pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.6+; validate it with pydantic.

However, creating models from nested JSON manually is still not a lot of fun. Save valuable time and create models with a few clicks using jsontopydantic.com. All you need to do is copy the JSON response to get pydantic models generated.

Once you have the models generated you can do some fine-tuning:

  • Double-check input types and leverage some awesome pydantic data types if needed such as SecretStr or PaymentCardNumber
  • Add some class configs arbitrary_types_allowed or the also popular orm_mode

Once you have everything in place, you can simply unpack the response into the model and convert it back to JSON using the original aliases with ease:

response = requests.get("https://restcountries.eu/rest/v2/alpha/de")model = Model(**response.json())print(f"Borders: {model.borders}")
print(f"Flag URL: {model.flag}\n")
print(f"JSON Serialization: {model.json(by_alias=True, exclude={'borders'})}\n")

Like in the previous blogpost I also created a small interactive example for you to try:

Send API requests from VSCode with Thunder Client

Many know Postman as an essential tool when interacting with web APIs. Now you can also do the same seamlessly within VSCode using the Thunder Clientextension. Especially for beginners, this is a great way to also do more complex API calls that require authentication, params, and certain headers:

TLDR

I hope you enjoyed this quick blog post about some handy tools that made my life a lot easier when interacting with APIs. In a nutshell here’s what we learned:

  1. Inspect requests from a browser and quickly turn them into pythonic requests using Curl to Requests.
  2. Save some tpying and generate validated pythonic data models with jsontopydantic.com.
  3. Experiment and get started with APIs right from VSCode with the awesome Thunder Client.

Looking for more tips and tricks? Check out my recent blogpost on handling dirty text data:

--

--

Felix Vemmer
Geek Culture

Operational Intelligence Business Analyst at N26 looking to build his skills in Data Science.