Over a million developers have joined DZone.

Marvel's Superheroic REST API

DZone's Guide to

Marvel's Superheroic REST API

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

Recently Marvel comics released a REST api that allows developers to access comic book characters and their associated comics and stories. I know how cool is this! This post will take you through some sample code and show you how to get information back from their API.

Here are the pre-requistes needed before proceeding.

Firstly, setup a pom.xml file for maven and setup two dependencies. We will use resty as our HTTP client and Apache's common codec to simplify the process of hashing our parameters. Create a pom.xml file that looks like the following.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





Now that the dependencies are set, let's create a simple REST client that exercises one of the services. Realistically if you can interact with one end point, you should be able to apply the same technique to interact with the others.

Understanding the URL structure

From the documentation, the url to get all the characters is this http://gateway.marvel.com/v1/public/characters. We can't call this directly, we will need to pass in a few extra parameters for authorisation purposes.

Our url needs to look something like this:


Except the parameters need to be replaced with real values.

Let's breakdown the extra parameters we are passing in.

Parameter Value
timeStamp a timestamp (or other long string which can change on a request-by-request basis)
apiKey Your Public Key
hash md5 hash of the following fields concatenated (timeStamp + privateKey + publicKey)

Given the fact that we understand how requests need to be formulated, let's create a very simple REST client that invokes this service.

REST Client

Let's create the src directory structure.

mkdir -p src/main/java  

Now create a file with the path src/main/java/RestClient.java with the following contents.

import java.io.IOException;  
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.digest.DigestUtils;  
import us.monoid.web.Resty;

public class RestClient {  
    public static void main(String[] args) throws IOException {
        String publicKey = args[0];
        String privateKey = args[1];
        long timeStamp = System.currentTimeMillis();
        int limit = 5;

        String stringToHash = timeStamp + privateKey + publicKey;
        String hash = DigestUtils.md5Hex(stringToHash);

        String url = String.format("http://gateway.marvel.com/v1/public/characters?ts=%d&apikey=%s&hash=%s&limit=%d", timeStamp, publicKey, hash, limit);
        String output = new Resty().text(url).toString();

The following points need to be made about the code above.

  • The public key and private key are passed into the client program as the first and second argument respectively
  • The timestamp used is just the System time, so System.currentTimeMillis()
  • The DigestUtils from common-codec is used to hash the concatenated string
  • A limit of 5 is set to restrict the amount of data coming back
  • A simple text string is returned from the request and printed out to the console

With all this done, you should be able to compile and run it.

$ mvn clean install
$ java -classpath target/*.jar RestClient your-public-key your-private-key 

If this runs correctly, you should receive output like the following. I've truncated the output for brevity's sake.

{"code":200,"status":"Ok","etag":"3c514a4e7","data":{"offset":0,"limit":5,"total":1402,"count":5,"results":[{"id":1009521,"name":" Hank Pym",

Hopefully this post showed you the first steps towards integrating with the Marvel REST API. Good luck with making your Marvel super hero charged applications!

If you need any further assistance, the Marvel documentation is actually quite comprehensive and should be your first port of call.

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}