DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. RESTful API Design-Driven Approach

RESTful API Design-Driven Approach

In this tutorial, you will learn the fundamentals of RESTful API design by applying REST principles and best practices.

Alex Theedom user avatar by
Alex Theedom
·
Aug. 18, 20 · Tutorial
Like (7)
Save
Tweet
Share
15.42K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial I will show you the fundamentals of designing a RESTful API specification by applying REST principles and best practices, then you’ll be ready to try my online tutorial: How to design a REST API with API Designer?

If you already know what is meant by API in the context of RESTful web services, you can skip to the next section. If not, read on.

Level-Set on API

The abbreviation API stands for Application Programming Interface this in itself, does not help us understand what it is, however in the context of web services, it can refer to one of two things:

  1. The RESTful API specification is written using a modeling language such as Open API specification or RAML (RESTful API Modeling Language) that defines a contract for how software components can interact with a service.
  2. The implementation of a web service or microservice whose contract is designed by REST principles that describe how other services must interact with it.

In this post, I will use the first understanding of this term. Even though both are correct, the most technically relevant for this post is the first: an API is a contract for how software applications talk to each other.

Level-Set on REST

The acronym REST stands for REpresentational State Transfer. It is an architectural style used to represent the transmission of data from one application component to another. In the context of web services, we are talking about the representation of resources (i.e. data) transferred over HTTP by calling a URI that represents the data and via an HTTP method that represents the action to perform against the given data.

What Is RESTful API design?

RESTful API design is the activity of describing the behavior of a web service in terms of its data structures and the actions you allow other application components to perform on its data by the principles of REST. Those principles are covered later in this blog.

Why Design a RESTful API?

Imagine that you are an Architect (the kind the design building) and you set out to build an office block without a blueprint. You turn up on the first day with a truck full of bricks and some cement. What are the chances that you'll be successful and build a structure that conforms to code and more importantly, doesn't fall? It's about zero. Without a blueprint the chance of failure is high.

The same approach applies to web service development. You need a blueprint, or more appropriately, an API specification. This is necessary to evaluate the API design and solicit feedback before even starting to build the implementation. 

In addition to providing a specification for the web service’s development, an API contract serves to document its expected behavior, data types, and security requirements. 

You should now be satisfied that API design is necessary for a RESTful web service, and should start to wonder how is the best approach to actually designing an API specification.

API Design Tooling

The tooling chosen by an API designer has substantial influence over the designer’s productivity. Highly productive tools such as the Anypoint API Designer from MuleSoft is perfect for designing APIs with OAS (swagger) or RAML. 

RESTful API Specification Language

There is a choice of modeling languages available to the API designer, each with its approach to describing a RESTful API, but each respecting the REST architectural style. The most widely adopted options are:

  • RAML (RESTful API Modeling Language)
  • OAS (Swagger)
  • API Blueprint

In this post, I will design an API using RAML (the RESTful API Modeling Language).

Design a REST API With RAML

To design a contract for a RESTful API I need to define the behavior of the underlying services in terms of to REST architectural style, this means identifying the following aspects:

  • The endpoints (which represent the resource/data)
  • The HTTP Method (which represent the actions on the data)
  • The format of the HTTP request and response (in terms of the data structure)
  • Optional 
    • any security requirements encapsulated as HTTP headers (e.g. OAuth authentication headers)
    • HTTP caching requirements as HTTP headers

Use Case Context

Let's imagine that my organization has asked me to design an API that presents a representation of our employee’s data. The API should expose functionality that allows full CRUD capabilities for that employee data and the API specification should also document the user data type and provide examples.

Let’s get started by defining the header of the API specification.

Define the API Header

I must start with the header of the specification file. In the header, I define the title, the version, and the base URI.

YAML
 




x


 
1
#%RAML 1.0
2
title: Employees API
3
baseUri: http://example.com/api/{version}
4
version: v1


The base URI defines the absolute URL of the actual API implementation.

Define the Resources

Next, I want to define the endpoint URIs. These identify the resource that represents the employee data and against which CRUD functionality can be performed.

According to REST conventions, the resource URIs should be named as nouns that relate to the data it represents. The table below shows an example of how this might look for common resources: 

Data type

Resource URL

Employee data

/employees

Product data

/products

Flight data

/flights

If the resource represents a collection the nous should be pluralized. The expectation is that an invocation of the GET /employees endpoint returns a list of employees (even if the array contains only one employee). According to RAML syntax the resource URI it terminated with a colon :

YAML
 




xxxxxxxxxx
1


 
1
/employees:


Use HTTP Methods With Resource URIs

There are five HTTP methods typically used by designers of RESTful APIs. They are mapped to CRUD functionality as shown below: 

  • POST (mapped to Create)
  • GET (mapped to Read)
  • PUT (mapped to Update)
  • DELETE (mapped to Delete)
  • PATCH (mapped to Partial update)

According to REST principles, the GET and POST (read and create) methods are used with a resource representation that is a single URI resource because they don't target a specifically identifiable resource, such as GET /employees and POST /employees. For the DELETE, PUT and PATCH (delete, update, partial update) methods are used with a resource representation that includes a URI variable that identifies the resource being changed, such as DELETE /employees/{employeeID} and PUT /employees/{employeeID}.

YAML
 




xxxxxxxxxx
1


 
1
/employees:
2
  get:
3
  post:
4
  /{employeeID}:
5
    put:
6
    delete: 



Define HTTP Requests

An essential aspect of designing a RESTful API is to provide sufficient details for clients to know how to make validated requests.

For example the POST /employee/{employeeId} HTTP request is expected to have a body that contains the employees' details to be created in the target system. The API specification should include a data type definition and an example of employee data in a valid format.

It is also a convention to specify how the data will be represented. This is defined as a MIME type and can be one of many types including the most common types: JSON and XML

In the RAML below the employee GET, POST, and PUT endpoints are defined. You can see that an example of employee data is included in the specification as well as defining that the data is going to be formatted as JSON.

YAML
 




xxxxxxxxxx
1
20


 
1
/employees:
2
 get:
3
 post:
4
   body:
5
     application/json:
6
       example: |
7
         {
8
           "firstName" : "John",
9
           "lastname" : "Smith"
10
         }
11
 /{employeeID}:
12
   put:
13
     body:
14
       application/json:
15
         example: |
16
           {
17
             "firstName" : "John",
18
             "lastname" : "Smith"
19
           }
20
   delete:



Define HTTP Responses and HTTP Status Codes

HTTP Status Codes are sent with the response to an HTTP request and indicate the status of the requests in one of five categories. The first digit of the status code identifies the status of the response.

  • 1xx — Informational
  • 2xx — Success
  • 3xx — Redirection
  • 4xx — Client Error
  • 5xx — Server Error

Full details of all status codes can be found here HTTP Status Codes.

Let’s define a response for the GET and DELETE methods. Here I have defined a 200 code for the GET request to indicate success and a 204 response to indicate that the deletion was successful but the response body does not include a representation of the resource deleted.

YAML
 




xxxxxxxxxx
1
19


 
1
/employees:
2
 get:
3
   description: Retrieve the list of all employees
4
   responses:
5
     200:
6
       body:
7
         application/json:
8
           example: |
9
             {
10
               "firstName" : "John",
11
               "lastname" : "Smith"
12
             }
13
 post:
14
 /{employeeID}:
15
   put:
16
   delete:
17
     responses:
18
       204:
19
         description: The delete request has been successfully executed.



The Complete API Specification

Let’s put all the parts together and complete the API design:

YAML
 




xxxxxxxxxx
1
38


 
1
#%RAML 1.0
2
title: Employees API
3
baseUri: http://example.com/api/{version}
4
version: v1
5
 
          
6
/employees:
7
 get:
8
   description: Retrieve a list of all employees
9
   responses:
10
     200:
11
       body:
12
         application/json:
13
           example: |
14
             {
15
               "firstName" : "John",
16
               "lastname" : "Smith"
17
             }
18
 post:
19
   body:
20
     application/json:
21
       example: |
22
         {
23
           "firstName" : "John",
24
           "lastname" : "Smith"
25
         }
26
 /{employeeID}:
27
   put:
28
     body:
29
       application/json:
30
         example: |
31
           {
32
             "firstName" : "John",
33
             "lastname" : "Smith"
34
           }
35
   delete:
36
     responses:
37
       204:
38
         description: The delete request has been successfully executed.



Conclusion

In this post I have covered just the foundations of RESTful API Design with RAML, you have seen how to define resources, methods, requests, and responses. If you want to learn more about designing APIs I can highly recommend the tutorial: How to design your first API with API Designer.

API REST Web Protocols Data (computing) Web Service Design microservice Data Types Requests application

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Full Lifecycle API Management Is Dead
  • Reliability Is Slowing You Down
  • Monolithic First

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: