How to access to Marvel’s API with marvel 0.1.0 wrapper in Python

But first…. what is an API?

Published in
7 min readSep 17, 2021

--

What is an API?

API is the acronym for Application Programming Interface. It is a connection between computers or between computer programs. It simplifies software development and innovation by enabling applications to exchange data and functionality easily and securely.

How an API works

APIs communicate through a set of rules that define how computers, applications or machines can talk to each other. APIs sit between an application and the web server, acting as an intermediary layer that processes data transfer between systems.

Here is how it works:

  1. A client application initiates an API call to retrieve information (also known as request). This request is processed from an application to the web server via the API’s Uniform Resource Identifier (URI) and includes a request verb, headers, and sometimes, a request body.
  2. After receiving a valid request, the API makes a call to the external program or web server.
  3. The server sends a response to the API with the requested information.
  4. The API transfers the data to the initial requesting application.

While the data transfer will differ depending on the web service being used, this process of requests and response all happens through an API. Whereas a user interface is designed for use by humans, APIs are designed for use by a computer or application.

API calls usually include authorization credentials to reduce the risk of attacks on the server, and an API gateway can limit access to minimize security threats. Also, during the exchange, HTTP headers, cookies, or query string parameters provide additional security layers to the data.

Types of APIs

There are 4 main types of web API: Open APIs, Partner APIs, Internal APIs and Composite APIs. In this work we are using an Open web API which are open source application programming interfaces you can access with the HTTP protocol. Also known as public APIs, they have defined API endpoints and request and response formats.

SOAP & REST API protocols

As the use of web APIs has increased, certain protocols have been developed to provide users with a set of defined rules that specifies the accepted data types and commands.

  • SOAP (Simple Object Access Protocol) is an API protocol built with XML, enabling users to send and receive data through SMTP and HTTP. SOAP API is typically associated with the enterprise world, has a stricter contract-based usage, and is mostly designed around actions.
  • REST (Representational State Transfer) is a set of web API architecture principles, which means there are no official standards (unlike those with a protocol). Is typically used for public APIs and is ideal for fetching data from the web. It’s much lighter and closer to the HTTP specification than SOAP.

Now that we have learn about the APIs, lets begin with Marvel’s API.

Create awesome stuff with the world’s greatest comic API!

The Marvel Comics API allows developers everywhere to access information about Marvel’s vast library of comics — from what’s coming up, to 70 years ago.

General Info about Marvel’s API

The Marvel Comics API is a RESTful service which provides methods for accessing specific resources at canonical URLs and for searching and filtering sets of resources by various criteria. All representations are encoded as JSON objects.

The data covered is primarily information from Marvel-related comic books, including:

  • 1491 Characters
  • 43759 Comics
  • 6200 Creators
  • 10713 Series
  • 96740 Stories
  • 75 Events

Service Endpoint

The Marvel Comics API’s base endpoint is http(s)://gateway.marvel.com/.

Resources

You can access six resource types using the API:

  • Comics: individual print and digital comic issues, collections and graphic novels.
  • Comic series: sequentially numbered (well, mostly sequentially numbered) groups comics with the same title.
  • Comic stories: indivisible, reusable components of comics.
  • Comic events and crossovers: big, universe-altering storylines.
  • Creators: women, men and organizations who create comics.
  • Characters: the women, men, organizations, alien species, deities, animals, non-corporeal entities, trans-dimensional manifestations, abstract personifications, and green amorphous blobs which occupy the Marvel Universe (and various alternate universes, timelines and altered realities therein).

We can also get images!

Marvel comics are famous for their iconic images, and we have provided a number of options for developers in order to optimally present images to your application’s users. As always, consult the terms of use and follow the attribution guidelines when displaying images in an application or website.

How to get the API key

First of all you have to sign in or create an account on the developers official Marvel website

Developer portal

Once you have sign in you have to go to My Developer Account tab to get your public and private key.

Great! Now that you have your API keys it’s time to check the marvel 0.1.0 wrapper!

Marvel 0.1.0 wrapper

Click on marvel 0.1.0 to access the webpage. Once you click on it you will see this:

Marvel Pypi

In this page you’ll find the wrapper description, installation, usage and some examples.

The objects and sub resources from the wrapper are:

Each of the above objects returns back the appropriate response (json) as mentioned in the official documentation of Marvel’s API. Give it a look!

As an example, on this project we are making the call for Characters resource.

On the documentation of Marvel’s API we can make this call like:

GET /characters

And also we can make these other calls from the resource Characters:

Marvel API documentation for Character resource

The marvel 0.1.0 wrapper makes the calls easier for the Character resource by using just one function: characters. This function has the following sub resources:

  • all
  • get
  • comics
  • events
  • series
  • stories

This sub resources do the same requests as the one shown on the previous image and the parameters of each sub resource are the parameters that each of the Marvel’s API calls have.

As said before, we are making calls for Characters which means we are using the characters.all() function from the wrapper. If we want to get information about characters by Id then we use the characters.get() function from the wrapper and so on.

The parameters for the function characters.all() are the same as the GET/characters call:

Characters parameters

The parameters for the function characters.get() are the same as the GET/characters/{characterId} call:

Character by Id parameters

And so on… :)

Let’s make the requests!

Start by installing the marvel 0.1.0 wrapper with the pip:

pip install marvel

The call for the Characters resource is made! Let’s find all the characters which name starts with “Captain”

The request returns a json response

To get a better visualization of the name of the characters we can run the following code

If we want to see all the information we must create a data frame

Now lets select a character Id to test characters.stories() function, as an example we are selecting Captain America ID #1009220

Now let’s try the characters.comics() function

Lets try to get a comic full image! Check the Marvel IMAGES documentation and then select a comic. These are all the comics titles:

How about comic #11: Captain America (2018) no.30? To get the path of the image we have to get the thumbnail from the df_comic dataframe.

Geat! So now what we have to do is select a size and ratio. Since we want a full-size image, the path will be:

http://i.annihil.us/u/prod/marvel/i/mg/1/40/60d5e76879154.jpg

And we will see this image

Path

Now try it yourself and have fun with the Marvel’s API!

References

--

--

Mathematician and Data Scientist Student from University of Sonora