什么是API端点?(为什么它们如此重要?)

杰米·尤维勒(Jamie Juviler)
杰米·尤维勒(Jamie Juviler)

Published:

API促进为客户问题提供解决方案所需的数据。要跟上竞争对手,您需要了解API是什么,它们如何与您的内容策略一起使用以及API和API端点为您的网站或产品带来的功能。

man using a computer to access an api endpoint

一个API,应用程序编程接口的缩写是一系列规则,允许应用程序与外部开发人员共享其数据。用最明确的话来说,API使您可以采用“他们的东西”并与“您的东西”一起使用。在这种情况下,他们的东西位于API端点。

在本指南中,我们将分解API端点是什么,端点在集成中扮演了什么角色,以及如何开始使用公共API(并可能与您自己的)进行测试。

立即下载:35个代码模板[免费摘要]

To fully understand this definition and where endpoints fit in the API model, let’s briefly review how APIs work.

For two software applications to通过互联网集成, one application — called the client — sends a request to the other application’s API. The client may request a resource from the app’s database or ask to perform an action on the server.

接收和验证客户端的请求后,API执行请求的操作,然后将响应发送给客户端。此响应包括请求的状态(例如,已完成或拒绝)和客户要求的任何资源。bob体育苹果系统下载安装

diagram of apis and api endpoints in the API request/resposne process

APIs typically allow access to many different resources on a server. For example, a social network’s API might let clients retrieve and modify post content, user profiles, and images. A news site’s API will allow access to its article content, authors, and media like podcasts and videos.

知道这一点,客户如何指定他们要在要求中访问的资源?

这answer is by using the correct endpoint. In their requests, clients specify an endpoint as a URL. This URL tells the server, “The resource I want is at this location.”

该过程类似于您在浏览器中访问网页的方式。Web浏览器通过将URL发送到Web服务器来加载Web页面,并且服务器使用请求的页面响应。同样,客户端需要正确的端点URL来从API请求特定资源。

端点与API

重要的是要注意,端点和API是不同的。端点是API的组成部分,而API是一组规则,允许两个应用程序共享资源。bob体育苹果系统下载安装端点是资源的位置,API使用端点URL来检索请求的资源。bob体育苹果系统下载安装

API端点示例

开发人员列出了其API的所有端点文档因此,用户知道如何访问所需的资源。bob体育苹果系统下载安装但是这些终点在实践中是什么样的?让我们看看领先平台的一些现实示例。

Twitter API端点示例

Twitter API暴露有关推文,直接消息,用户等的数据。

假设您想检索特定推文的内容。为此,您可以使用Tweet查找端点,有URLhttps://api.twitter.com/2/tweets/ {id}(在哪里{ID}是推文的唯一标识符)。

现在,say you want your website to stream public tweets in real-time so your visitors stay informed on a specific topic. You can use Twitter’sfiltered stream endpoint, whose URL ishttps://api.twitter.com/2/tweets/search/stream

We'll look at the filtered stream endpoint in more detail later in this post.

Spotify API Endpoint Example

Spotify的API使开发人员访问歌曲,艺术家,播放列表和用户数据。例如,如果您想get a specific album,您可以访问Spotify目录中的任何专辑https://api.spotify.com/v1/albums/{id}(在哪里{ID}是专辑的唯一标识符)。

或者,说您想发送一个请求让用户遵循播放列表。在这种情况下,发送带有端点的看请请求https://api.spotify.com/v1/playlists/ {playlist_id }/followers(在哪里{playlist_id}是播放列表的唯一标识符)。

YouTube API端点示例

YouTube API除其他外,还可以轻松地将YouTube视频嵌入到任何网站上。当您转到YouTube视频并复制嵌入式代码时,您会从YouTube的API中请求视频。

通过YouTube的API获取视频的另一种方法是从端点请求它们https://www.googleapis.com/youtube/v3/videos,返回符合您请求中指定的参数的视频列表。

为什么API端点很重要?

One of the first questions you ask about APIs is: Why do so many businesses share their data openly, for free?

大多数情况下,答案是比例。随着公司的发展,这些公司内的员工意识到他们拥有的想法要比有时间和资源更多的想法来开发它们。bob体育苹果系统下载安装通常,与专门从事这些想法的其他外部公司合作比内部更容易。

通过创建API,公司允许第三方开发人员构bob全站app建改善其平台采用和使用的应用程序。这样,企业可以建立一个生态系统,该生态系统依赖于其API的数据,这通常会带来额外的收入机会。

HubSpot API作为另一个例子:通过通过API公开我们的软件的功能和数据,开发人员可以将bob电竞官方下载其应用程序与我们的技术集成在一起,从而导致app ecosystem这增加了我们的影响力,使希望与HubSpot一起使用其他应用程序的客户更容易。

据我们了解到,从字面上看,端点是API的核心。它们是客户端和服务器通信的地步。如果没有正确结构和功能的终点,API充其量会混淆并在最坏情况下破裂。当您通过API提供更多数据时,至关重要的是要确保每个端点为客户提供宝贵的资源。bob体育苹果系统下载安装

How to Test API Endpoints

在讨论Web API时,我们通常在谈论API的类型called aREST API, which utilizes HTTP methods that tell the API what action to take. The four most common HTTP methods in API requests are:

  • 得到:检索资源
  • 邮政:创建资源
  • :更新现有资源
  • 删除:删除资源

让我们看看如何使用Twitter API提出请求filtered stream endpoint。请求通过编写HTTP方法进行格式,然后是端点URL。因此,对被过滤的流端点的请求看起来像:邮政https://api.twitter.com/2/tweets/search/stream

现在,想象一下,您想在Twitter上了解有关HubSpot的信息。您想从@HubSpot帐户一旦发布。另外,假设您只需要包含文章链接的推文。

在这种情况下,使用过滤的流端点是一个理想的选择。但是,为了使API知道要发送哪些推文,您需要定义过滤条件。否则,您将要求查看实时发布的每条推文(Gulp)。

我们可以以规则形式将过滤标准应用于端点。为了构建这些规则,您将使用一组操作员。对于此示例,您可以使用两个Twitter API操作员 -从:and有:链接- 仅查看包含链接的某些帐户的推文。要指示过滤后的流端点以显示包含链接的帐户的推文,请使用以下规则:从:集线器有:链接

根据您的要求,您将使用HTTP方法邮政。除了在请求中包括上述规则外,您还将包括内容类型和授权。内容类型下面定义为“应用程序/JSON”,因此请求以数据格式JavaScript对象表示法(JSON)呈现。

这re are several online tools available for testing an API endpoint. Here we’ll use卷曲,一个支持HTTP的命令行工具。它可以提出请求,获取数据并发送数据,因此很棒tool for testing APIs

Here’s what your request to the Twitter API should look like on the command line. To authenticate your request, you’ll have to replace the placeholder text$BEARER_TOKENwith your app’s unique bearer token which is available in your developer portal.

curl -X POST 'https://api.twitter.com/2/tweets/search/stream/rules' \

-h“ content-type:application/json” \

-h“授权:持有人$ bearer_token” -d \

'{

“添加”: [

{"value": "from:HubSpot has:links"}

这是给予的

}'

这Future of APIs in Business

We live in a world that now expects open and available content for all — the natural progression of this is for publishers themselves to release their own APIs so that customers can develop applications with them.

API sharing applies to all businesses: not just those that are web-based, but rather anyone who has a web-based tool or component of their organization. Of course, this concept will cause hurdles for some organizations, a major one being getting everyone on the same page of how APIs work.

It’s easy to get bogged down in the technical jargon of APIs. However, when applied to real-life cases, it’s easier to understand how and APIs work the way they do. Hopefully, you now have a better grasp of one of their key components, endpoints.

编者注:该帖子最初于2021年9月发表,并已更新以进行全面性。 coding templates

相关文章

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our隐私政策

您可以立即开始使用数十个免费编码模板。