Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

REST API Basic Guidelines: Design It Right

DZone's Guide to

REST API Basic Guidelines: Design It Right

Learn about the six REST constraints: uniform interface, client server, stateless, cacheable, microservices, and code-on-demand.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

This article is featured in the new DZone Guide to Integration, Volume III. Get your free copy for more insightful articles, industry statistics, and more.

Your data model has started to stabilize and you're in a position to create a public API for your web app or device.

You realize it's hard to make significant changes to your API once it's released and want to get as much right as possible up front. Now, the internet has no shortage on opinions on API design.

Good REST API design is extremely hard, especially when considering that an API actually represents a contract between you and those who consume your data. Breaking this contract may have a very bad impact starting with time being wasted in development and ending with many angry users with mobile apps or services that no longer work.

But, since there's no one widely adopted standard that works in all cases, you're left with a bunch of choices: What formats should you accept? How should you authenticate? Should your API be versioned?

Let's first start with the basic guidelines.

What Is REST?

Representational State Transfer (REST) is a technical description of how the World Wide Web works. If you imagine that the Web is a device, and it could have an operating system, its architectural style would be REST.

REST defines a set of architectural principles by which one can design Web services that focus on a system's resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages. If measured by the number of Web services that use it, REST has emerged in the last few years alone as a predominant Web service design model. In fact, REST has had such a large impact on the Web that it has mostly displaced SOAP- and WSDL-based interface design because it's a considerably simpler style to use.

Rest Constraints

A well-designed REST API entices developers to use the web service and is today a must-have feature. But how do you clearly define that the API is actually RESTful? REST architecture describes six constraints, and we are going to describe them below in this article.

By conforming web applications, web services, and web APIs with proven REST constraints, teams can create very scalable, simple, maintainable, evolvable, and reliable systems.

Here are the REST six constraints:

  1. Uniform Interface

  2. Client Server

  3. Stateless

  4. Cacheable

  5. Microservices

  6. Code-on-Demand

DZone_GuideToIntegration2016

Each constraint adds beneficial properties to the web system. By incorporating the constraints, teams can build simple, visible, usable, accessible, evolvable, flexible, maintainable, reliable, scalable, and performant systems.

Image title

Table 1: How following specific REST constraints will result in gaining valuable system properties.

Uniform Interface

The uniform interface that any REST service must provide is fundamental to its design. Its constraint defines the interface between clients and servers. The four guiding principles of the uniform interface are:

  • Resource-Based: Individual resources are defined in requests using URIs as resource identifiers and are separate from the responses that are returned to the client. REST Web service URIs should be intuitive to the point where they are easy to guess. Think of a URI as a kind of self- documenting interface that requires little, if any, explanation or reference for a developer to understand what it points to and to derive related resources. To this end, the structure of a URI should be straightforward, predictable, and easily understood.

  • Actions on Resources Through Representations: When a client gets a representation of a resource, including any metadata attached, it has enough information to customize or delete the resource on the server, if it has permission to do so. REST asks developers to use HTTP methods explicitly and in a way that's consistent with the protocol definition. This basic REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. According to this mapping:

    • To create a resource on the server, use POST.

    • To retrieve a resource, use GET.

    • To change the state of a resource or to update it, use PUT.

    • To remove or delete a resource, use DELETE.

  • Self-Descriptive Messages: Each client request and server response is a message, and RESTful applications expect each message to be self-descriptive. That means each message contains all the information necessary to complete the task. Other ways to describe this type of message are "state-less" or "context-free." Each message passed between client and server can have a body (or "entity body") and metadata. RESTful applications also operate on the notion of a constrained set of message types that are fully understood by both client and server. There are well-defined rules for how clients and servers are expected to behave when using these messages. The names and meanings of the messages' metadata elements (HTTP Headers) are also well-defined.
  • Hypermedia as the Engine of Application State (HATEOAS): Clients deliver the state via body contents, query-string parameters, request headers, and the requested URI. Services deliver state to clients via body content, response codes, and response headers. A hypermedia-driven site provides information to navigate the site's REST interfaces dynamically by including hypermedia links with the responses.
DZone_GuideToIntegration2016

Client Server

The uniform interface divides clients from servers. This means that, for instance, clients are not concerned with data storage, which remains internal to each server, so that the portability of client code is improved. Servers are not engaged with the user interface or user state so they can be simpler and more scalable. Servers and clients may also be replaced and developed independently, as long as the interface is not modified.

Stateless

There is no connection state. Interaction is stateless. Each new request should carry all the information required to complete it, and must not rely on previous interactions with the same client. The necessary state to operate the request is contained within it as a part of the URI, query-string parameters, body, or headers. The URI identifies the resource, and the body contains the state of it.

Cacheable

Resources should be cacheable whenever possible (with an expiration date and time). As the clients can cache responses, the protocol must allow the server to explicitly specify which resources may be cached, and for how long, in order to prevent clients from reusing state or inappropriate data in response to further requests:

  • Since HTTP is universally used as the REST protocol, the HTTP cache-control headers are used for this purpose.
  • Clients must respect the server's cache specification for each resource

Well-managed caching partially or completely eliminates some client-server interactions and improves the performance.

Microservices

There are different definitions of microservices, and searching the Internet provides many good resources that provide their own viewpoints and definitions. However, most of the following characteristics of microservices are widely agreed upon:

  • Encapsulates a customer or business scenario. What is the problem you are solving?
  • Developed by a small engineering team.
  • Written in any programming language and uses any framework.
  • Consists of code and (optionally) state, both of which are independently versioned, deployed, and scaled.
  • Interacts with other microservices over well-de ned interfaces and protocols.
  • Has unique names (URLs) used to resolve their location.
  • Remains consistent and available in the presence of failures. Microservices are applications composed of small, independently versioned, and scalable customer-focused services that communicate with each other over standard protocols with well-defined interfaces.

Code-on-Demand

Servers are able to temporarily extend or customize the functionality of a client by transferring logic to it that it can execute. Examples of this may include compiled components such as Java applets and client-side scripts such as JavaScript. This is the only constraint out of six that is optional.

Versioning

No matter what you are building, no matter how much planning you do beforehand, your core application is going to change, your data relationships will change, attributes will invariably be added and removed from your Resources. This is just how software development works, and is especially true if your project is alive and used by many people (which is likely the case if you’re building an API).

Getting RESTful API versioning right can have a major impact on the how your API is perceived by your API consumers internally or externally, and can also make the management of your API estate more dif cult if it’s ill conceived.

Conclusion

When starting to design and develop a RESTful API, you first must learn the constraints REST is enforcing to the system you are building. Violating any constraint other than Code on Demand means that service is not strictly RESTful. Complying with all constraints, and thus conforming to the REST architectural style, will enable any kind of distributed hypermedia system to have desirable emergent properties, such as performance, scalability, simplicity, modifiability, visibility, portability, and reliability.

Getting RESTful API versioning right can have a major impact on how your API is perceived by your API consumers internally or externally. Being aligned with the REST constraints will also help you with your API versioning both in your development when using the microservices constraint and in your design when creating the uniform interface constraint.

More EI Goodness

For more insight on application integration architectures, REST APIs, microservices, and more, get your free copy of the new DZone Guide to Integration, Volume III!

If you want to see other articles in the guide, check out:

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
integration ,rest ,api

Published at DZone with permission of Guy Levin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}