In this age of integrations, sharing data between systems is more important than ever.

Imagine an ecommerce website without integrations. On top of handling product listings, your site would need to build tools for processing payment, shipping, email automation, user accounts, and other functions all by itself. This isn’t a scalable solution — it’s more efficient to outsource these tasks to other services.

So, in order to work together, software applications use application programming interfaces, or APIs. APIs provide a standardized way for two applications to send data back and forth. Your ecommerce site can interact with payment software, shipping software, and any other necessary integrations through their respective APIs.

There are many approaches to building an API, but if you’re interested in introducing software integrations to your product, there’s one special method you should know:REST API. In this guide, I’ll explain what a REST API is and why they’re so useful.

Before getting started, you should be familiar withwhat APIs are and how they work.

Download Now: Free Intro Guide to HTML & CSS

What is a REST API?

来understand REST, we need to review some key terms first:

  • Aclientis the person or program using the API. The client makes requests to the API in order to retrieve some information or change something within the application. Your web browser is a client — it interacts with APIs different websites to get page content from them. The requested info is sent back to your browser and displayed on your screen.
  • Aresourceis any piece of information that the API can provide the client. For instance, a resource in Facebook’s API could be a user, a page, a photo, or a post. Each resource has a unique name, called the resource identifier.
  • Aserveris used by the application that receives client requests, and contains resources that the client wants. The server has an API to interact with clients without giving them direct access to content stored in its database.

Now for our definition. REST is a set of guidelines that software can use to communicate over the internet in order to make integrations simple and scalable. A REST API (also called a “RESTful” API) is a specific type of API that follows these guidelines.

休息代表Representational State Transfer. This means that when a client requests a resource using a REST API, the servertransfersback the currentstateof the resource in a standardizedrepresentation.

In other words, REST APIs work by fielding requests for a resource and returning all relevant information about the resource, translated into a format that clients can easily interpret (this format is determined by the API receiving requests). Clients can also modify items on the server and even add new items to the server through a REST API.

Let’s say I want to build a program that integrates with YouTube. My program (the client) can ask YouTube’s REST API for information about a specific video (a resource). YouTube’s API will respond to my request with the resource state, which includes attributes like the video name, publishing date, and view count, and video link, all packaged in a format that my program can quickly parse and use. My program could also post a video (i.e., add a new resource) to my personal YouTube channel through the API.

Now that we know the benefits of REST APIs, let’s go into detail about what makes REST APIs different from other types of APIs.

The Six Rules of REST APIs

来fully benefit from the functionality that REST provides, APIs must follow six requirements. (Well, technically five are required and one is optional.) Each requirement lays the groundwork for a fast and versatile API.

1. Client-Server Separation

Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.

By simplifying communication between clients and servers, RESTful APIs keep the two conveniently independent. This way, client software can grow their builds without worrying about affecting any other servers, and server contents can be modified without inadvertently affecting clients.

2. Uniform Interface

这条指导原则指出,所有请求和再保险sponses must follow a common protocol, or a way of formatting their messages. Applications and servers are written in all sorts of different languages that don’t do a great job of working together without an intermediary. A uniform interface is a common language for any client to communicate with any REST API.

Without standardized communication, translating requests and responses between software would be a total mess. Minor discrepancies would cause information to be jumbled and lost, and applications would have to update their request processes whenever APIs updated theirs. A uniform interface eliminates this possibility.

For most REST APIs, this common language is HTTP, orHyper-Text Transfer Protocol. HTTP wasn’t created specifically for REST. Rather, REST adopted this communication protocol as the standard for applications that use it.

来use HTTP with a REST API, the client sends a request in a specific format that might look familiar to you. For example, a request to the YouTube API for video data looks like this:

GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails

Like all requests to a REST API, this request contains two pieces of information:

  • GETis the HTTP method. This specifies the action the client wants to make on the resource. There four basic HTTP requests a client can make are:
    • 获取:检索资源。
    • POST: To create a new resource.
    • fut:编辑或更新现有资源。
    • DELETE: To delete a resource.
  • https://…is the URL. The URL contains the uniform resource identifier, or URI, which specifies the target resource.

In this case, the URL is also called anendpointbecause it is the location where the API actually interacts with the client.

接收和验证请求后,主机返回有关目标资源的信息。通常,该信息以称为JSON的格式回来,该格式代表JavaScript Object Notation. JSON lays out all the contents of a resource in a lightweight format that humans can easily read.

This page in YouTube's API documentationshows the format of a Youtube Video resource formatted in JSON. Can you identify some of the different resources provided?

3.无状态

All calls with a REST API must be stateless. This means that every interaction is independent, and each request and response provides all the information required to complete the interaction. Every request by the client is interpreted by the server as a brand new ask — the server remembers nothing about past requests.

Stateless transfers greatly reduce the amount of server memory needed and improve the odds of a successful response, since the server is not required to take additional action to retrieve old data. This ensures that these interactions are scalable: As software grows and makes more requests, developers don’t need to worry about significantly more memory being used, or overloading the server with requests.

4. Layered System

So far I’ve described API requests as a simple interaction between a client and server, but this is a bit of a simplification. In reality, there are typically more servers between these two entities. These servers, or layers, are there to add security, handle and distribute traffic, or assist with a number of other important functions.

This principle requires that messages between the client and target server should always be formatted and processed the same way, regardless of layers that sit between them. Additional layers should not affect client-server interactions.

当开发人员遵循此指南时,可以重新安排,更新或其他修改服务器系统,而不会影响核心请求响应。

5. Cacheable

访问网站时,将媒体存储在客户的设备上时,就会发生缓存。当客户端返回该站点时,缓存的数据会从本地存储中快速加载,而不是再次从服务器中获取。缓存可以节省服务器资源和带宽,同时减少页面bob体育苹果系统下载安装加载时间,这就是为什么大多数大型网站这样做的原因。

REST APIare created with data caching in mind. When a server sends its response to a client, the response should indicate whether the resource provided can be cached, and for how long.

6. Code on Demand (Optional)

The final REST principle is optional. If desired, an API can send computer code to clients in its response. This empowers the client to run the code in its own backend.

As long as an API adheres to this set of rules, it is considered RESTful. However, these rules leave plenty of room for developers to customize the functionality of their API. This flexibility distinguishes REST APIs from another common web API method, the Simple Object Access Protocol (SOAP).

REST被认为是肥皂更简单,更有效的替代品,因为它需要较少的代码来完成任务,并且遵循比SOAP较少的刚性结构和逻辑。此外,休息设置了用于API设计的护栏,但为开发人员构建API留下了许多选择。

Why use REST APIs?

The REST framework was introduced by computer scientist Roy Fielding in 2000, and today it shapes how we view, modify, and transfer content online. Many of the most popular web and cloud companies use REST APIs for their applications, including Facebook, YouTube, Twitter, and Google.

但是为什么要休息?基本上,这是Web应用程序的绝佳系统。这是这种API的主要好处:

  • REST APIare flexible.They can handle many types of requests and send data in many different formats.
  • REST APIare scalable.They are designed for communication between any two pieces of software, regardless of size or capability. As a web application grows and adds more resources, its REST API will be able to quickly handle the increasing amount and variety of requests.
  • REST APIincorporate existing web technologies,使他们相对容易构建和使用。来request a resource via a REST API, you just need to provide its URL.

How to Use a REST API

Web applications with publicly available APIs will have documentation available on the “developers” section of their websites. Here you’ll find instructions on how to access and use the API in conjunction with your own software. If the API is built with REST principles, the documentation will probably indicate this.

许多API需要一个API key使用。API键是API提供商向开发人员授权访问其API的独特字符串。API密钥通常与客户端请求一起发送,以识别服务器的客户端。保持您的API密钥私有。如果您的钥匙落入了错误的手中,则可以用来代表您做一些不太好的事情。

来test out a public API, use a tool that can structure HTTP requests, likePostman. Try a variety of different HTTP methods and URLs, and see what happens.

REST API Examples

You can find REST APIs all over the web — you’ve likely used some today without realizing it. Here are a few examples:

Twitter

The Twitter API lets third-party applications read and write data. Use it to write and post tweets, share tweets, and read profiles. This API is especially effective for downloading and analyzing large amounts of tweets about specific topics.Learn more about using Twitter’s API in our guide.

Instagram

The Instagram Basic Display API offers access to profile information, photos, and videos.You can utilize this APIand others to build apps that pull this user information and integrate it into your own product. Instagram also has aGraph API可用于专业的Instagram帐户来管理其在线活动。

Spotify

Spotify’s web API allows clients to request information about artists, songs, albums, and playlists on its platform. You can also use it to add songs to playlists, pause and play music, shuffle songs, anda lot more.

HubSpot

All of HubSpot’s APIs are made with REST conventions and designed for robust integrations that help businesses get the most value out of HubSpot’s tools. You can add advanced functionality to HubSpot’s powerful marketing software and sync your HubSpot account with other useful tools.

更多其他美联社I examples that you can use for your business, check out our list of our favoritefree and open APIs for marketers.

Don’t Sleep on REST

It’s a common belief that REST APIs will soon be the industry standard for web-based communications, and for good reason. They enable any two online applications to interact and share data, regardless of their sizes or capabilities. Through REST, a small startup can interface with a massive government agency, and vice versa.

Software tools can create amazingly powerful and innovative systems by working together, which is something any growing online platform should want to be a part of. If you’re looking to connect your app to the software world, don’t sleep on REST.

新的呼吁行动

css introduction

Originally published Jul 27, 2020 7:00:00 AM, updated October 28 2021

来pics:

Application Programming Interface (API)