Laravel - How to remove API from URL

While developing an API in Laravel I needed to get rid of /api path used to access the API endpoints.

Each section covers a specific and supported version of Laravel.

Laravel 11

Go to the bootstrap folder and open the app.php file.

Here are the parameters currently passed into the withRouting method.

->withRouting(
    web: __DIR__ . '/../routes/web.php',
    api: __DIR__ . '/../routes/api.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)

Add in the apiPrefix parameter, set it to a blank string, and then remove the web parameter as it wouldn’t be needed anymore.

->withRouting(
    api: __DIR__ . '/../routes/api.php',
    apiPrefix: '',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)

Test your API routes after the change to make sure that the change has been applied as expected.

Laravel 10

Open the app folder, inside the app folder open the Providers folder, and then open the RouteServiceProvider.php file.

Locate the API code which looks like the following.

$this->routes(function () {
    Route::middleware('api')
        ->prefix('api')
        ->group(base_path('routes/api.php'));

    Route::middleware('web')
        ->group(base_path('routes/web.php'));
});

Remove the prefix method call and the prefix will be removed from the project URL.

$this->routes(function () {
    Route::middleware('api')
        ->group(base_path('routes/api.php'));

    Route::middleware('web')
        ->group(base_path('routes/web.php'));
});

Version 9 or below

If you are using Laravel 9 or below then I highly recommend that you upgrade the project as at the time of writing this guide version 9 has reached the end of its life and won’t receive any more security updates.

https://laravel.com/docs/11.x/releases#support-policy