Summer BF 2
Save up to 80 % on our C# e-learning courses. Only this week!
Get up to 80 % extra points for free! More info

Lesson 2 - REST API, SOAP, GRAPH, and JSON

In the previous lesson, Introduction to Node.js, we installed and introduced Node. Today we're going to talk about the different types of APIs and the JSON format, which will prepare us for the project we'll be developing in this course.

Project Introduction

In this tutorial, we'll be creating a simple backend API to manage a movie database. We'll be able to add, edit or delete movies, and, of course, view them. APIs are the most common project types in Node.js. We'll access the movie database using a RESTful API, and we'll be using the JSON format... wait, wait, what do all these abbreviations mean?


API stands for Application Programming Interface. It's generally anything that allows individual parts of a software to communicate with each other. It's a set of functions and procedures, classes, communication protocols, and other tools. The key is that the communication methods are precisely defined. A programmer who creates a new system component then knows (or can find in the documentation), which procedures he can use to make his component work properly with the other ones.

If you want to think of the API as something of everyday life, imagine a waiter in a restaurant that mediates (and translates) communication between a guest and the chef. Or as a car dashboard that uses precisely defined methods (buttons on the dashboard) to pass what the driver (one component) wants from another component (the engine). And the car instructions from the car manufacturer are like the API documentation, where the various methods (buttons) are described.

There are graphical APIs, framework and library APIs, operating system APIs, but we'll be mainly interested in web APIs.

Web APIs

A web API defines how some components communicate over the Internet. Typically, these are two parts of one application (a website retrieves the necessary data from the server using an AJAX request) or two different applications (a mobile app downloads data from a website). A web service can be a synonym to that. Our web API will allow to perform operations over the movie database, such as searching for an existing movie or inserting a new movie. Of course, web APIs are not limited to databases; we can send SMS through them, check the weather and so on.

A Web API Sample

A small but popular API for e-shops is at…

When you open the link, you will see the exchange rate for the current day. Notice that no HTML page was generated, just raw JSON data. Because web APIs are not intended for people, but for programs. The API output looks like this:


Types of Web APIs

There are several popular ways of communicating on the Web. These are:

  • Simple APIs
  • APIs using the SOAP protocol
  • APIs using the REST architecture
  • Graph API from Facebook

Simple APIs

We've already introduced a simple JSON API with the exchange rate sample. CSV or XML formats can also be used for simple APIs.

Simple APIs typically offer only some data list to download, such as the weather by city, and don't allow more sophisticated data manipulation.


SOAP stands for Simple Object Access Protocol. Messages sent using SOAP are typically XML-based (a markup language similar to HTML). Compared to REST (see below), SOAP is rather procedural (REST is data-oriented). This is also reflected in how we call the API - the URL when using SOAP will typically contain a verb, unlike REST, where there will typically be some noun (in our case, it will be movies as you'll see later).

A SOAP request would look like this without the long header I left out:

<xs:element name="Body" type="tns:Body"/>
<xs:complexType name="Body">
<xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
 Global Attributes.  The following attributes are intended to be
  usable via qualified attribute names on any complex type referencing
<xs:attribute name="mustUnderstand" type="xs:boolean" default="0"/>
<xs:attribute name="relay" type="xs:boolean" default="0"/>
<xs:attribute name="role" type="xs:anyURI"/>
 'encodingStyle' indicates any canonicalization conventions
  followed in the contents of the containing element.  For example, the
  value '' indicates the pattern
  described in the SOAP Version 1.2 Part 2: Adjuncts Recommendation
<xs:attribute name="encodingStyle" type="xs:anyURI"/>
<xs:element name="Fault" type="tns:Fault"/>
<xs:complexType name="Fault" final="extension">
<xs:documentation> Fault reporting structure </xs:documentation>
<xs:element name="Code" type="tns:faultcode"/>
<xs:element name="Reason" type="tns:faultreason"/>
<xs:element name="Node" type="xs:anyURI" minOccurs="0"/>
<xs:element name="Role" type="xs:anyURI" minOccurs="0"/>
<xs:element name="Detail" type="tns:detail" minOccurs="0"/>
<xs:complexType name="faultreason">
<xs:element name="Text" type="tns:reasontext" minOccurs="1" maxOccurs="unbounded"/>
<xs:complexType name="reasontext">
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang" use="required"/>
<xs:complexType name="faultcode">
<xs:element name="Value" type="tns:faultcodeEnum"/>
<xs:element name="Subcode" type="tns:subcode" minOccurs="0"/>
<xs:simpleType name="faultcodeEnum">
<xs:restriction base="xs:QName">
<xs:enumeration value="tns:DataEncodingUnknown"/>
<xs:enumeration value="tns:MustUnderstand"/>
<xs:enumeration value="tns:Receiver"/>
<xs:enumeration value="tns:Sender"/>
<xs:enumeration value="tns:VersionMismatch"/>
<xs:complexType name="subcode">
<xs:element name="Value" type="xs:QName"/>
<xs:element name="Subcode" type="tns:subcode" minOccurs="0"/>
<xs:complexType name="detail">
<xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
 Global element declaration and complex type definition for header entry returned due to a mustUnderstand fault
<xs:element name="NotUnderstood" type="tns:NotUnderstoodType"/>
<xs:complexType name="NotUnderstoodType">
<xs:attribute name="qname" type="xs:QName" use="required"/>
 Global element and associated types for managing version transition as described in Appendix A of the SOAP Version 1.2 Part 1 Recommendation
<xs:complexType name="SupportedEnvType">
<xs:attribute name="qname" type="xs:QName" use="required"/>
<xs:element name="Upgrade" type="tns:UpgradeType"/>
<xs:complexType name="UpgradeType">
<xs:element name="SupportedEnvelope" type="tns:SupportedEnvType" minOccurs="1" maxOccurs="unbounded"/>

Unfortunately, SOAP APIs are not very simple, even though they have the word Simple in their name. Rather, they are used in the state sector and finance for robust projects and are not a frequent choice for classic applications.


REST is currently a very popular interface architecture. It stands for REpresentational State Transfer - a term that Roy Fielding introduced in his dissertation. He is one of the co-authors of the HTTP protocol, so it's not surprising that REST uses this protocol. REST implements four basic CRUD operations. These operations are Create, Read, Update, and Delete. In the HTTP protocol, they are represented by the methods:

  • GET (read)
  • POST (create)
  • PUT (update)

Using different HTTP methods is the basic principle of the REST API. For example, we should not remove data through the classic GET request that we use when we are trying to open a page.

Thanks to these four methods, the REST interface is easy to understand and use. Compared to SOAP, it's much more brief and, therefore, more efficient. Despite its briefness, each request contains all the information needed to handle it, so the server doesn't have to hold any state (it's stateless). It follows, among other things, that if the application needs a state, it must be held by the client.

An API that uses the REST interface is called a RESTful API.

Graph API

To get an idea that there are other application interfaces for communication over the web, let's take a look at the Graph API. It was created and popularized by Facebook, which serves very diverse data through it. Facebook can be asked so many things that the REST or even SOAP requirements would be very confusing. The Graph API uses the concept of social graphs with vertices and edges (as in graph theory) to represent information. Vertices are objects such as a user, photo, page, or comment. The edges are then connections between these objects such as comments below a photo. And the data is stored in objects' fields.

The sample query below asks Facebook about a user's birthday, email, and city:{your-user-id}

We use the Graph API when it's difficult to formalize our queries, and in each question it's necessary to say exactly what we are interested in.

Our Application's API

We'll use the RESTful API for our application for the reasons mentioned above (simplicity, briefness) and also because we need to perform the operations which it's designed for on our movies. Another reason is that although REST supports many formats, the JSON format is the most popular.

Today we'll talk a little about the JSON format. For the start, I'll mention just that it's suitable for web browsers, and it will be practical for us also because of the database we're going to use - we'll discuss this in one of the next lessons.

When submitting a request using the RESTful API, the correct use of the path helps us to process the request as we need it. For our first table, which will include movie data, we'll use the following paths in our app (we'll always replace id with the appropriate identifier):


For POST request (create, corresponds to the CREATE CRUD method) the address will be:


(no need to enter the movie id, it's about creating an item in the database, the database will create the id itself)


For the GET request (read, corresponds to the READ CRUD method) we'll use this address for all movies:


And for one specific movie:



For the PUT request (updating, corresponds to the UPDATE CRUD method) the address will be as follows:



And for the DELETE request (corresponds to the DELETE CRUD method) we'll use this address:



I've already mentioned that the RESTful API uses mainly the JSON format. So what is it exactly?

JSON (stands for JavaScript Object Notation) is a format used to store data and communicate on the Web. Although it's based on JavaScript, it's also used by other languages. Its syntax is very similar to the syntax of objects and arrays in JavaScript, with several limitations. Property names must always be in double quotation marks, and only simple data is allowed - no calculations or function calls. Comments are not allowed.

Here's a short demonstration of the JSON format representing a musician, his band and its albums:

        "name": "Trent",
        "last name": "Reznor",
        "band name": "Nine Inch Nails",
        "born": 1965,
        "male": true,
        "albums": [
            "Pretty Hate Machine",
            "The Downward Spiral",
            "The Fragile",
            "With Teeth",
            "Year Zero",
            "to be continued..."

In the next lesson, Running the project and first lines in Express, we'll explore the Express library. We'll go through creating a new project and implementing the first of the RESTful API methods, the GET method.


Previous article
Introduction to Node.js
All articles in this section
Article has been written for you by Petr Sedlacek
Do you like this article?
No one has rated this quite yet, be the first one!
Activities (7)




To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!