<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stoplight</title>
    <description>The latest articles on DEV Community by Stoplight (@stoplight).</description>
    <link>https://dev.to/stoplight</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F1043%2F11fe8253-effa-49a8-b76d-d214fc38f64b.png</url>
      <title>DEV Community: Stoplight</title>
      <link>https://dev.to/stoplight</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stoplight"/>
    <language>en</language>
    <item>
      <title>API Design Patterns for REST Web Services</title>
      <dc:creator>Adam DuVander</dc:creator>
      <pubDate>Mon, 08 Jun 2020 20:57:12 +0000</pubDate>
      <link>https://dev.to/stoplight/api-design-patterns-for-rest-web-services-43i0</link>
      <guid>https://dev.to/stoplight/api-design-patterns-for-rest-web-services-43i0</guid>
      <description>&lt;p&gt;REST turns 20 years old this year. In addition to the architecture and recommendations outlined in Roy Fielding’s dissertation, we now have two decades of practical application. When designing APIs, it makes sense to build upon the best practices already implemented by countless others.&lt;/p&gt;

&lt;p&gt;This post identifies the most common REST API design patterns across several categories. Rather than start anew, build upon this foundation of API guidelines from thousands of successful API companies.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Methods and Status Codes
&lt;/h2&gt;

&lt;p&gt;By the strict definition of REST, you don’t need to use the HTTP protocol. However, the two developed alongside each other, and almost every RESTful API relies upon HTTP. For that reason, it makes sense to structure your API around the built-in methods and status codes that are already well-defined in HTTP.&lt;/p&gt;

&lt;p&gt;Each HTTP request includes a method, sometimes called “HTTP verbs,” that provides a lot of context for each call. Here’s a look at the most common HTTP methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;: read data from your API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt;: add &lt;em&gt;new data&lt;/em&gt; to your API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt;: update &lt;em&gt;existing data&lt;/em&gt; with your API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PATCH&lt;/strong&gt;: updates a &lt;em&gt;subset of existing data&lt;/em&gt; with your API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt;: remove data (usually a single resource) from your API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you design your API, you’ll want to rely on the methods to express the primary purpose of a call. For that reason, you don’t want to use a POST to simply retrieve data. Nor would you want a GET to create or remove data.&lt;/p&gt;

&lt;p&gt;Much as these methods provide the request context from client to server, HTTP status codes help describe the response in the reverse direction.&lt;/p&gt;

&lt;p&gt;Some common HTTP status codes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;200&lt;/strong&gt;: Successful request, often a GET&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;201&lt;/strong&gt;: Successful request after a create, usually a POST&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;204&lt;/strong&gt;: Successful request with no content returned, usually a PUT or PATCH&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;301&lt;/strong&gt;: Permanently redirect to another endpoint&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;400&lt;/strong&gt;: Bad request (client should modify the request)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;401&lt;/strong&gt;: Unauthorized, credentials not recognized
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;403&lt;/strong&gt;: Forbidden, credentials accepted but don’t have permission&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;404&lt;/strong&gt;: Not found, the resource does not exist&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;410&lt;/strong&gt;: Gone, the resource previously existed but does not now&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;429&lt;/strong&gt;: Too many requests, used for rate limiting and should include retry headers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;500&lt;/strong&gt;: Server error, generic and worth looking at other 500-level errors instead&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;503&lt;/strong&gt;: Service unavailable, another where retry headers are useful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many more HTTP status codes and methods to consider, but the above lists should get you well on your way for most APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Friendly Endpoint Names
&lt;/h2&gt;

&lt;p&gt;A typical design pattern with REST APIs is to build your endpoints around resources. These are the “nouns” to HTTP method verbs. Your API design will be much easier to understand if these names are descriptive.&lt;/p&gt;

&lt;p&gt;For example, if you’re working on a cookbook API, you might include the following endpoint:&lt;br&gt;
&lt;code&gt;/recipes/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you add new recipes, you would POST them to the endpoint. To get a list, you use the GET method on the same endpoint. To retrieve a specific recipe, you could call it by its identifier in the URL:&lt;br&gt;
&lt;code&gt;/recipes/42&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;One thing to specifically avoid with friendly REST endpoint names is describing actions. For example, a verb within the endpoint (i.e., &lt;code&gt;/getRecipes/&lt;/code&gt;) would run counter to relying on HTTP to provide that context.&lt;/p&gt;

&lt;p&gt;Our &lt;a href="https://stoplight.io/blog/crud-api-design/"&gt;CRUD API Design Recommendations&lt;/a&gt; goes into more detail, including popular topics like plurals and versioning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Support Use Cases with API Parameters
&lt;/h2&gt;

&lt;p&gt;Naive or simplistic API design can follow all the guidelines above and still not support the use cases that developers will need. It’s important to thoroughly understand how an API will be used and get feedback from collaborators, such as with &lt;a href="https://stoplight.io/mocking"&gt;mock API servers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Often, when use cases are discovered after an API is built, engineers will create new endpoints to support these unearthed requirements. For example, your cookbook API may need to return only recipes from a specific category, or you want to show the recipes with the least prep time. Rather than create redundant endpoints, plan for smart parameters from the start.&lt;/p&gt;

&lt;p&gt;There are three common types of parameters to consider for your API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Filtering&lt;/strong&gt;: Return only results that match a filter by using field names as parameters. For example: &lt;code&gt;/recipes/?category=Cookies&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pagination&lt;/strong&gt;: Don’t overload clients and servers by providing everything. Instead, set a limit and provide &lt;code&gt;prev&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt; links in your response. Example: &lt;code&gt;/recipes/?limit=100&amp;amp;page=3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sorting&lt;/strong&gt;: Provide a way to sort or some use cases will still require paging through all results to find what’s needed. Example: &lt;code&gt;/recipes/?sort=prep_time&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three approaches can be used together to support very specific queries. For example, this API request would retrieve one cookie recipe with the shortest preparation time: &lt;code&gt;/recipes/?category=Cookies&amp;amp;sort=prep_time&amp;amp;limit=1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In some cases, you’ll need additional parameters or a special syntax to fully support API consumer expectations. You will likely want to provide a sort direction (i.e., &lt;code&gt;order=desc&lt;/code&gt; or &lt;code&gt;sort=prep_time:asc&lt;/code&gt;), and may have times when you want to filter or sort by multiple fields. Understanding your use cases will help determine the complexity of your parameters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Borrow From Existing Conventions
&lt;/h2&gt;

&lt;p&gt;While this post does its best to cover overall API design patterns, you’ll want to look at standards and conventions specific to your industry or a specific feature. Very few of us are building completely unique APIs, so there is a lot to learn from others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stoplight.io/blog/rest-api-standards-do-they-even-exist/"&gt;Many API standards&lt;/a&gt; are built around REST APIs. When you implement authentication for your API, for example, don’t blaze a new trail. There are many options, including the well-trod OAuth path, when providing user-associated data. You’ll find standards for API headers and a handful around data formats like JSON and XML, among others.&lt;/p&gt;

&lt;p&gt;You may be &lt;a href="https://stoplight.io/blog/designing-apis-for-microservices/"&gt;designing microservices APIs&lt;/a&gt;, which has its own set of considerations. Everything covered in this post likely still applies, but you’ll want to pay extra careful attention when designing microservices. Each will need to make sense on its own, yet benefit from a combination (loose coupling).&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;a href="https://stoplight.io/blog/open-banking-guide/"&gt;open banking APIs&lt;/a&gt; require their own treatment. European standards are the most mature and have a set of design patterns based around those regulations.&lt;/p&gt;

&lt;p&gt;Your industry may have its own set of standards or conventions. Even if they aren’t as strict as banking regulations, it’s worth giving proper consideration to a pattern with which developers will already be familiar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Document with An OpenAPI Definition
&lt;/h2&gt;

&lt;p&gt;As you design your API, it will be extremely useful to maintain an OpenAPI definition as the source of truth. This format, the next generation of the older Swagger file, describes endpoints, request data, responses, error codes, and more. In addition, it can be used to automate with tooling across the API lifecycle.&lt;/p&gt;

&lt;p&gt;Perhaps the most common use of an OpenAPI document is to &lt;a href="https://stoplight.io/documentation/"&gt;generate API documentation&lt;/a&gt;, especially an API reference. Since the format outlines the ways an API can be called, it contains all the information a developer needs to integrate with the API. Plus, some API references don’t include essential details like error codes, so OpenAPI encourages accurate documentation. Further, you can generate new docs every time your API changes, so they’ll always be up-to-date.&lt;/p&gt;

&lt;p&gt;You can also use your OpenAPI definition to &lt;a href="https://stoplight.io/mocking/"&gt;create mock HTTP servers&lt;/a&gt;, which allows you to try out your API before you write any code. Circulate the interface amongst your team for early feedback, or validate the requests from your API client.&lt;/p&gt;

&lt;p&gt;Those are just two potential uses for your machine-readable API definition, which you can create OpenAPI definition files using YAML or JSON. Or, create them much faster with a visual OpenAPI editor. &lt;a href="https://stoplight.io/studio/"&gt;Stoplight Studio&lt;/a&gt; can read existing OpenAPI files from any git repo, and you can make edits—or start from scratch—within a beautiful editing environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Style Guides for Consistency
&lt;/h2&gt;

&lt;p&gt;Some design patterns are a matter of preference. Ideally, you can codify your organization’s approach once, rather than revisiting it each time you create an API. A style guide can keep your company on the same page with API design. In addition to being consistent between APIs, it’s even more important to maintain consistency within a single API.&lt;/p&gt;

&lt;p&gt;Some organizations will create a written API style guide. A document that is easily accessible within your intranet helps everyone understand the design patterns you’ve already adopted. However, you can go even farther by enforcing your style guide programmatically. Using a tool like an &lt;a href="https://stoplight.io/open-source/spectral"&gt;open source linter&lt;/a&gt;, you can define rulesets for your OpenAPI documents.&lt;/p&gt;

&lt;p&gt;When you &lt;a href="https://stoplight.io/blog/api-style-guide/"&gt;automate your API style guide&lt;/a&gt;, you can look for any number of API characteristics: resource and field names, capitalization formats, how you use punctuation, and versioning, among others.&lt;/p&gt;

&lt;p&gt;Your style guide, whether written or programmatic, becomes your own guidelines for the design patterns covered here. Help ensure your organization uses HTTP methods correctly, returns appropriate status codes, implements friendly endpoint names, uses smart parameters, and borrows from the existing conventions you’ve already identified.&lt;/p&gt;

&lt;p&gt;Now you’re ready to create fantastic APIs, so join the world’s leading API-first companies on &lt;a href="https://stoplight.io/"&gt;Stoplight’s API design management platform&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>api</category>
      <category>apidesign</category>
      <category>rest</category>
    </item>
    <item>
      <title>API Development Demystified</title>
      <dc:creator>Adam DuVander</dc:creator>
      <pubDate>Mon, 23 Mar 2020 15:04:11 +0000</pubDate>
      <link>https://dev.to/stoplight/api-development-demystified-2nc1</link>
      <guid>https://dev.to/stoplight/api-development-demystified-2nc1</guid>
      <description>&lt;p&gt;It’s clear that APIs are an important part of modern software. Companies frequently develop APIs to share data, functionality, and business processes. However, it’s not always clear where the backend API development ends and work begins on the website, app, or other clients to use the API.&lt;/p&gt;

&lt;p&gt;In fact, the most efficient organizations don’t require such a distinct hand-off. When your APIs are built thoughtfully, teams can work in parallel. In this post, we’ll look at the benefits of a forward-thinking API approach and how it is impacted across the full lifecycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Build APIs?
&lt;/h2&gt;

&lt;p&gt;It’s important to remember the benefits that APIs provide for enterprises. The alternative is large, all-knowing applications, which are slow to develop. In many cases, teams duplicate efforts, because there is not a method to communicate that a problem has already been solved elsewhere. This lack of knowledge limits a company’s ability to work with external parties, as well.&lt;/p&gt;

&lt;p&gt;APIs enable collaboration at all levels. A simple example is the interaction between frontend and backend teams. With an interface defined, neither team needs to wait on the other’s work. Rather than sequential efforts, they can work simultaneously. And, in true collaboration, the process can be iterative, sharing what they’ve learned to inform the next version. The same approach works within departments, across business units, and with partner companies.&lt;/p&gt;

&lt;p&gt;You can also stop reinventing the wheel. When an API already exists, teams can build upon others’ work. For example, an API to access a customer’s account details could be useful for a website, a mobile app, and many other consumers. With appropriate permissions controls in place, the same API could even be used in a partner’s application.&lt;/p&gt;

&lt;p&gt;Indeed, empowering partnerships is a major benefit to public APIs. However, internal API development can even enable external collaboration. Once you make internal teams aware of an API, they may see opportunities with partner companies.&lt;/p&gt;

&lt;h2&gt;
  
  
  The API Lifecycle
&lt;/h2&gt;

&lt;p&gt;Depending on the terminology you use, API development may refer to the entire lifecycle of an API or one phase within it. Either way, you’ll want to understand each phase and how they work together. While APIs help you move faster as an organization, that doesn’t mean you should create APIs without a thoughtful process. Plus, even hurried APIs will go through the API lifecycle, possibly with more headaches along the way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoplight.io%2Fimages%2Fapi-lifecycle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoplight.io%2Fimages%2Fapi-lifecycle.png" alt="API Lifecycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gartner defines the API development process as three major phases: Design, Build, and Run. Too often, teams will think of API development as only the build phase. It’s a natural misconception because that’s when the bulk of the code is written. However, that skips over the important design phase. There’s a lot more than code that goes into developing an API for the long term.&lt;/p&gt;

&lt;p&gt;As discussed in &lt;a href="https://stoplight.io/blog/api-integration-testing/" rel="noopener noreferrer"&gt;testing across your API lifecycle&lt;/a&gt;, there are three other important phases: Maintain, Support, and Update. While not necessarily sequential, every API will need these additional phases after it is pushed to production.&lt;/p&gt;

&lt;p&gt;As you look to update an API, you end up back where any API development should start: with API design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design-First API Development
&lt;/h2&gt;

&lt;p&gt;In the &lt;a href="https://stoplight.io/api-design-guide/basics/" rel="noopener noreferrer"&gt;API design guide&lt;/a&gt; we discuss the “design-second oxymoron.” It’s during the design phase that important decisions are made about how your API works and what it makes possible. Good API development practices will start with a collaborative design phase.&lt;/p&gt;

&lt;p&gt;When designing an API, you’ll need to keep teams on the same page about the decisions you make. The industry has rallied around the OpenAPI specification as a way to detail REST APIs. Sometimes referred to by the outdated term Swagger, OpenAPI is a document format to describe API endpoints and their related data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoplight.io%2Fimages%2Fstudio%2Fhero.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoplight.io%2Fimages%2Fstudio%2Fhero.png" alt="Stoplight Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stoplight.io/studio/" rel="noopener noreferrer"&gt;Stoplight Studio&lt;/a&gt; is a visual API design editor, which helps you quickly produce OpenAPI documents without memorizing syntax or writing any code. By describing an API during the design phase, teams can make important decisions about reusable data models, which HTTP methods to support, and how to handle error conditions.&lt;/p&gt;

&lt;p&gt;OpenAPI is a machine-readable format that can help you in the later phases of your API development, as well. As you build your API, you can &lt;a href="https://stoplight.io/p/docs/gh/stoplightio/prism/docs/guides/01-mocking.md" rel="noopener noreferrer"&gt;generate mock servers&lt;/a&gt; using your OpenAPI document as the source of truth for decisions made during design. These definitions can also create documentation and serve as a central object when discussing updates to your APIs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stoplight.io/studio/" rel="noopener noreferrer"&gt;Get started with API design&lt;/a&gt; and create your first OpenAPI document or start with an existing repository. You’ll build and run better APIs as a result.&lt;/p&gt;

&lt;p&gt;♻️ This post was originally posted on the &lt;a href="https://stoplight.io/blog/" rel="noopener noreferrer"&gt;Stoplight Blog&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
