Create you first API by example of IMDB parser

Published at: March 14, 2018, 2:18 p.m. in the Documentation

In this tutorial, you'll learn how to create your first API on the Smappi platform.

For this you need to register and confirm your Email and phone number via SMS.
After registration, you must upload SSH Keys once.

Creating an API

To do this, click on the Create new API button on the main page or in the Marketplace.
Next, enter the name of the API, for example "MovieDB" and a summary description of the project.

Smappi will create your Git repository and launch the container with your API.
After creating, you will see the address of the Git repository, the SSH key created earlier is used for authorization.
In the future, you can find the address of the repository on the page of your project, in the "Source" tab.

Writing an API

Clone the repository and edit the file "api.js":

const { jquery, DOM, request } = require('smappi');

 * Get info by movie
 * For example id="tt0113243"
 * @param {String} id
 * @example
 *   movie("tt0113243");
 *   // => {name: 'Hackers', year: '1995', datePublished: '1996-06-13', duration: '1h 47min', director: 'Iain Softley', writer: 'Rafael Moreu', ...}
function movie (id) {
    let obj = {}, t = o => o && o.text().trim();
    let response = request.get('' + id + '/');
    let $ = jquery(DOM(response.content));
    obj.rating = t($('span[itemprop="ratingValue"]')); = t($('div[class="originalTitle"]').clone().children().remove().end());
    if (! = t($('h1[itemprop="name"]').clone().children().remove().end());
    obj.year = t($('span[id="titleYear"] a'));
    obj.description = t($('div[class="summary_text"]'));
    obj.duration = t($('div[class="subtext"] > time[itemprop="duration"]'))
    obj.datePublished = t($('meta[itemprop="datePublished"]').attr('content'));
    obj.director = t($('span[itemprop="director"] span[itemprop="name"]'));
    obj.writer = t($('div[class="credit_summary_item"] span[itemprop="creator"] span[itemprop="name"]'));
    obj.poster = t($('img[itemprop="image"]').attr('src'));
    return obj;

module.exports = { movie };

So the first method of your API is ready, it allows you to get a brief information about the movie from the site "".
As you can see, everything is very simple.

Local development and debugging of your API

To do this, you need to set project dependencies, as well as Smappi SDK:

npm install
sudo npm install -g smappi

Now start the local server for development:

smappi run 8001
> Smappi Server running at http://localhost:8001/

Go to URL http://localhost:8001/movie?id=tt0113243 and see the following:

"rating": "6.2",
"name": "Hackers",
"year": "1995",
"description": "This movie is about hackers who are blamed for making a virus that will capsize 5 oil tankers.",
"duration": "1h 47min",
"datePublished": "1996-06-13",
"director": "Iain Softley",
"writer": "Rafael Moreu",
"poster": ",0,182,268_AL_.jpg"

Great, it works!


In order not to load the site, we will configure the caching, edit the file "caches.txt":

* 60        # to response any method of your API the cache will live 60 seconds
movie 3600  # for the movie method, you can specify one hour

Send all the changes to Smappi

git commit -m "Create movie method, which parse information of the movie"
git push

After that, you will be asked to add news about the changes in your API.
In the same time, Smappi will run the tests, build documentation for you from the docstrings (JSDoc).