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.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

How to Create a REST API with Laravel Lumen

Updated on December 8, 2021

5 Min Read
create rest api laravel

A Restful API is an application program interface that uses HTTP demands to GET, PUT, POST and DELETE information.

Lumen has the same foundation as Laravel, and many of the same components. But Lumen is built for microservices, not so much for user-facing applications. Such as frontend amenities like Bootstrap and Elixir. The authentication bootstrap and sessions do not come enabled out of the box, and there’s less adaptability for extending and changing the bootstrap records.

Why build a REST API in Lumen

  • Lumen is blazing fast.
  • It can handle more requests per second than Laravel.
  • It is a faster version for creating a full web application framework.
  • 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 the Cloudways platform, you get Laravel in a 1-click install with an optimized stack and pre-installed Composer.

With the introduction of mobile development and JavaScript frameworks, using a RESTful API is the leading choice to build a single interface between your information and your client. Laravel is a PHP framework developed with PHP designer efficiency in mind.

Cloudways Installation

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 :

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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 :

> composer require "laravel/lumen"  //installing Lumen
> vendor/bin/lumen new mylumen_car_app  //creating Lumen app

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.

> composer create-project laravel/lumen car_api

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:

> php artisan make:migration create_table_cars --create=cars

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:

Schema::create('cars', function (Blueprint $table) {

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:

> php artisan migrate
Migration table created successfully.
Migrated: 2016_09_08_142212_create_table_cars

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:

<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
 	protected $fillable = ['make', 'model', 'year'];	 

The Controller

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

namespace App\Http\Controllers;
use App\Car;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class CarController extends Controller{

	public function createCar(Request $request){
    	$car = Car::create($request->all());
    	return response()->json($car);
	public function updateCar(Request $request, $id){

    	$car  = Car::find($id);
    	$car->make = $request->input('make');
    	$car->model = $request->input('model');
    	$car->year = $request->input('year');
    	return response()->json($car);

	public function deleteCar($id){
    	$car  = Car::find($id);
    	return response()->json('Removed successfully.');

	public function index(){
    	$cars  = Car::all();
    	return response()->json($cars);

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.

$app->group(['prefix' => 'api/v1','namespace' => 'App\Http\Controllers'], function($app)



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

Testing It Out

Now, test it all out using curl.


> curl -i -X POST -H "Content-Type:application/json" http://<unique-to-you> -d '{"make":"toyota", "model":"camry", "year":"2016"}'


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).

> curl -H "Content-Type:application/json" -X PUT http://<unique-to-you> -d '{"make":"toyota", "model":"supra", "year":"1999"}'



Now, I will delete id = 1.

> curl -X DELETE http://<unique-to-you>

“Removed successfully.”


This is what the final data should look like:

> curl http://<unique-to-you>


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



This article highlights Lumen, a Laravel based micro framework that is optimized for REST API. We created the model, controller and the view for the CURL based app. The aim is to explain and demonstrate how using REST API with Laravel Lumen can optimize your application, that too within a few easy steps.If you have any problems following the code or would like to contribute to the discussion, please leave a comment below.

Both Laravel and Lumen perform well for cutting edge mobile and web applications. Due to the presence of a large community, and forming RESTful APIs with Lumen, Laravel is more compelling.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]


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

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now