Wednesday, May 1, 2024

How to Design an API Application Programming Interface Best Practices

api design best practices

That can be a tremendous advantage in the deployment of development staff resources. In many contexts, we will require a 200 response indicating success. 400 responses can often be corrected on the client side and the request resubmitted. An exception is 511, Network Authentication Required issued by a proxy controlling server access. Some API suppliers provide dedicated libraries for client-side developers that effectively encapsulate the HTTP REST API calls. It may be less effective for B2B APIs that require or return vast quantities of data.

api design best practices

Whenever you're ready

However, if you don’t dig deeper, you are designing an API based on your system. Do the details come from a user in the field, or are they passed through an online form? Ask the same questions about the other potential endpoints.

Supporting the world’s most-used database engine through 2050

I always imagine that the HTTP verb describes the action (what we'd like to do) and the URL itself (that points towards a resource) the target. "GET /api/v1/workouts" is also more fluent in human language. It doesn't make much sense to use verbs inside your endpoints and is, in fact, pretty useless. Generally each URL should point towards a resource (remember the box example from above). A little downside of just throwing is that we don't get a stack trace. But normally this error throwing would be handled by a third party library of our choice (for example Mongoose if you use a MongoDB database).

Assign proper API versioning

You just have to put it as a parameter between the actual path and our workout controller. Pagination is another mechanism to split our whole collection of workouts into multiple "pages" where each page only consists of twenty workouts, for example. This technique helps us to make sure that we don't send more than twenty workouts at the same time with our response to the client.

api design best practices

Maybe your customer needs more data in a single page of results than usual, or the payload has way more data than their app requires. You can’t create a one-size-fits-all solution, but you also don’t want a reputation for building APIs that are limiting. Here are 3 simple options to make your endpoints more flexible. Imagine ordering a “ready-to-assemble” table online, only to find that the delivery package did not include the assembly instructions. You know what the end product looks like, but have little to no clue how to start assembling the individual pieces to get there.

You can build fully customizable tests with built-in coverage reporting with Stoplight OpenAPI testing. Before developers and architects used a description document to help them design APIs, documentation was the biggest use case. While OpenAPI allows for much more than generated documentation, that remains a huge advantage to having your API described in OpenAPI.

Group associated resources together (logical nesting)

In our example we're not using a real database such as MongoDB or PostgreSQL because I'd like to focus more on the best practices itself. Therefore we're using a local JSON file that mimics our Database. But this logic can be transferred to other databases of course. The whole business logic will be in the Service Layer that exports certain services (methods) which are used by the controller. We'll build a REST API for a CrossFit Training Application. I've merged all those learnings (good and bad) together into one digestible article while providing a practical example that can be followed along.

What Is an API (Application Programming Interface)? - TechTarget

What Is an API (Application Programming Interface)?.

Posted: Mon, 24 Jan 2022 22:42:21 GMT [source]

We're extracting "mode" from the req.query object and defining a parameter of workoutService.getAllWorkouts. This will be an object that consists of our filter parameters. During the last sections we focused on improving our developer experience and how our API can be interacted with.

Model APIs with Accuracy

This example demonstrates how to handle a POST request by extracting data (name and email) from the request body. The route then sends a JSON response confirming the creation of a user, using a 201 status code which indicates that a resource was successfully created. POST requests are typically used to create or update resources.

Just be consistent in your design choices across all of your APIs, and be very clear in your documentation about anything that might be peculiar or nonstandard. As you design your APIs using OpenAPI, you’ll need to help your entire team and program conform to your chosen specification. Then, you can use automated linting tools to validate your JSON or YAML as you write so that it adheres to your style guidelines. An accurate API description is important so that you can feel confident that other tools will interpret your API the way you expect. There are different types of documentation, but OpenAPI-generated docs thrive for API references and interactive documentation. As you add and update your API endpoints, you can automatically keep your documentation updated.

Having verbs in our API endpoint paths isn’t useful and it makes it unnecessarily long since it doesn’t convey any new information. For instance, some like ‘get’ and some like ‘retrieve’, so it’s just better to let the HTTP GET verb tell us what and endpoint does. This is probably one of the best practices you need to keep in mind when designing your API.

No comments:

Post a Comment

14 best curling wands Tried-and-tested 2024

Table Of Content Best budget hairspray ghd Creative Curl Wand - UK 3-pin plug Consider the shape of the wand or barrel. Soft Waves Best Weat...