This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Create a REST API with Laravel Lumen

Updated on  3rd November

5 Min Read
Reading Time: 5 minutes

Lumen is a micro-framework built on top of Laravel. The framework is ideal for small apps and services that are optimized for speed. The most important application of the framework is to build REST APIs.

rest api with lumen

Why build a REST API in Lumen

  • Lumen is blazing fast.
  • It can handle more requests per second than Laravel.
  • It uses nikic/FastRoute instead of Symfony, thereby increasing performance.

To build a REST API with Lumen, signup to launch a server on Cloudways Laravel hosting. On Cloudways platform, you get Laravel in a 1-click install with an optimized stack and pre-installed Composer.

laravel launch gif


You might also like: Quickly Host Laravel on DigitalOcean in Few Clicks

Note: If you want to use the Laravel Stack

Inside the Laravel public_html directory, run the following commands to create a Lumen app inside the Laravel project :

From here you have 3 options to update the webroot:

Symlink your Lumen public directory to the Laravel public directory.

OR update the webroot using the Cloudways Application Settings page.

OR move Lumen’s index.php inside the Laravel public directory and

update the bootstapp/app.php path.

Next, inside the app’s public_html folder, run the following command to create a Lumen project.

Change the webroot by adding /public  (this is the public folder for all Laravel apps) in the Cloudways Application Settings page so that the new webroot is now public_html/public.

Load up the application URL in the browser and you will see the following page.


MySQL connection

I will use MySQL for this tutorial. Update the DB credentials in the .env file using the MySQL ACCESS available on the Cloudways Application Page. The following fields should be updated:

Next, uncomment the following lines in bootstrap/app.php

The Facade class is a static interface to classes available in the application’s service containers. This class is required to access certain core components of Lumen. Eloquent is the ORM that is used  communicate with the MySQL database.


It is now time to create the database schema.

Create a table for Cars with four fields including the auto- increment id. The other three fields are make, model, and year. To create the table, run:

This will create a <date>_create_table_cars.php file inside the database/migrations/ folder. I will now edit this file and define the table.

Add the following code inside up function:

The up function will be triggered when the table is actually created during the migration. The down function (no changes required in this function) will delete the table if the need arises.

Now run the migration using:

At this point, the table has been created and can be viewed in MySQL manager.


The Model

Next step is the creation of the model. Create the app/Car.php file and add the following code:

laravel banner cta

The Controller

Create a controller inside the app/Http/Controllers/CarController.php.

The routes

Now all that remains is the addition of the routes. I will write routes for creating, updating, deleting and viewing cars.

Open up app/Http/routes.php and add the following routes.

Notice that I have grouped the routes together into a common api/v1 route.

Testing It Out

Now, test it all out using curl.


After adding several more records, the DB would look like:



Next,I will update id = 1 to Toyota Supra 1999 (the previous valve was Toyota Camry 2016).


Now, I will delete id = 1.

“Removed successfully.”


This is what the final data should look like:

As a final confirmation,I can verify the data using the Cloudways MySQL manager:


You might also like: Create ToDo App With Authentication Using Lumen


This article highlighted Lumen, a Laravel based micro framework that is optimized for REST API. I created the model, controller and the view for the CURL based app. The aim of this tutorial is to show how easy it is to create REST API with Lumen. If you have any problems following the code or would like to contribute to the discussion, please leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.