Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leostereo's avatar

use web or api route when building an api.

Hi guys, after learning laravel im ready to build my own api. One thing I would like to ask is: Why whould I use api routes if I can also return jsons using web routes ?

Regards; Leandro.

0 likes
2 replies
fylzero's avatar

@leostereo The difference is the way they auth out of the box. Take a look at app/Providers/RouteServiceProvider.php

If you are building an api... you should use the api routes. Using the api routes also prefixes your routes by default with /api. Where this gets really confusing is when you are building an SPA and want to use the API calls with your application and also develop a publicly documented API, or private developer API...

Basically, I would argue that you can plan this however you want. In general I tend to add my own files to RouteServiceProvider for internal ajax calls. I usually create an ajax.php and put my SPA routes there, put my API routes in API and my usually one or two web routes in web.php

24 likes
piljac1's avatar

Main differences between web and API routes:

  1. API routes are prefixed with /api

  2. API routes are throttled to avoid abusive numbers of requests in a short sequence

  3. API routes are stateless, which means:

Stateless means there is no record of previous interactions and each interaction request has to be handled based entirely on information that comes with it.

Please or to participate in this conversation.