DEV Community

Cover image for Building REST APIs with Dart and Shelf (2025 Guide)
Andrew James
Andrew James

Posted on

Building REST APIs with Dart and Shelf (2025 Guide)

Welcome to the new era of backend development. In 2025, the demand isn't just for powerful servers, but for high-performance, lightweight APIs that can scale effortlessly to meet the demands of a global audience. The era of monolithic backends is giving way to a leaner, more agile approach—and for developers who live and breathe Dart, the answer is no longer a world away.

Step out of the Flutter-only mindset and discover a backend ecosystem that is fast, flexible, and fundamentally Dart. A Framework in programming provides a foundational structure, and unlike bloated frameworks that weigh down your projects with unnecessary abstractions, Shelf is a minimalist web server middleware designed for performance and composability. It gives you the raw power to build a server from the ground up, with only the tools you need and nothing more.

This is your definitive 2025 guide to building a REST API with Dart and Shelf. We’ll skip the fluff and dive straight into the code, showing you how to go from a blank canvas to a fully functional API. By the end, you won't just understand the code—you’ll have a new perspective on what's possible with Dart, from front to back.

Building REST APIs with Dart and Shelf

REST APIs have become the backbone of modern web and mobile applications, and in 2025, Dart has matured into a compelling choice for building them. While Flutter is the undisputed king of Dart on the front end, the server-side ecosystem has seen significant growth, with lightweight and powerful frameworks like Shelf leading the way.

Shelf is not a full-fledged framework like Express.js or Laravel; rather, it’s a web server middleware that makes it easy to create and compose web servers. Its minimalist, composable nature makes it an excellent choice for building small to medium-sized REST APIs, especially when you need performance and a low footprint.

This guide will walk you through building a simple REST API from scratch using Dart and Shelf, providing a complete, working example that you can use as a foundation for your own projects.

Step 1: Setting Up Your Project

First, ensure you have the latest stable version of the Dart SDK installed. You can check your version by running dart --version in your terminal.
Create a new Dart project from your command line:

Next, open the pubspec.yaml file and add the necessary dependencies. We'll use shelf for the core server and shelf_router for a clean, declarative way to handle routing. We'll also use shelf_io to run the server on a port.

Save the file and run dart pub get in your terminal to download the packages.

Step 2: Creating the API Logic

For this example, we'll build a simple API to manage a list of in-memory products. We'll define a simple Product class to represent our data.
Create a new file lib/api.dart and add the following code.

This sets up our data model and an in-memory list that will act as our "database."

Step 3: Defining Endpoints with shelf_router

Now, let's create the API's router and handlers. A handler is simply a function that takes a Request and returns a Response.
In lib/api.dart, let's add our router and API handler functions.

This is the core of our API. We define handlers for each HTTP method and path, which process the request and return a JSON-formatted response. The shelf_router package automatically extracts path parameters like .

Step 4: Running the Server

Finally, let's bring it all together and run our server. Open the bin/my_api.dart file and replace its contents with the following code.

Run your server from the terminal:

You should see the message: Server listening on http://localhost:8080.
You can now use a tool like Postman, Insomnia, or a simple curl command to test your API.

Example curl commands:

GET all products:
curl http://localhost:8080/products

GET a single product:
curl http://localhost:8080/products/1

POST a new product:
curl -X POST -H "Content-Type: application/json" -d '{"name": "Dart Mug", "price": 14.99}' http://localhost:8080/products

PUT to update a product:
curl -X PUT -H "Content-Type: application/json" -d '{"price": 15.99}' http://localhost:8080/products/3

DELETE a product:
curl -X DELETE http://localhost:8080/products/2

The Big Picture: Shelf vs. Other Dart Frameworks (2025)

While Shelf is powerful and flexible, the Dart backend ecosystem has evolved to offer other choices that might be better suited for different projects.

Dart Frog: Built on top of Shelf, Dart Frog provides a more opinionated, file-system-based routing framework. It's an excellent choice for a Flutter developer looking for a familiar feel on the backend. If your project needs to scale beyond a simple API and you want a more structured approach, Dart Frog is a natural next step.

Serverpod: For large-scale, data-intensive applications, Serverpod is a full-stack, open-source framework with a focus on code generation and performance. It's a complete solution that handles client-side and server-side code, database migrations, and more. It offers a much higher level of abstraction and is ideal for complex applications.

Conclusion
In 2025, Dart and Shelf offer a lean, fast, and productive way to build REST APIs. Its minimalist design gives you full control, while a rich ecosystem of packages (like shelf_router) provides the tools you need to build robust applications. For projects that require more structure, you can seamlessly migrate to a higher-level framework like Dart Frog or Serverpod. Start with Shelf, master its principles, and you'll be well-equipped to tackle any backend project in the Dart ecosystem.

Top comments (0)