---
title: 'Your first API | Laravel API for beginners #1'
source: 'https://youtube.com/watch?v=mLA8mNjLrgA'
video_id: 'mLA8mNjLrgA'
date: 2026-06-17
duration_sec: 0
---

# Your first API | Laravel API for beginners #1

> Source: [Your first API | Laravel API for beginners #1](https://youtube.com/watch?v=mLA8mNjLrgA)

## Summary

This video is the first episode of a Laravel API course for beginners, covering how to set up a Laravel project for API development and create a basic JSON endpoint.

### Key Points

- **Course Overview & Prerequisites** [0:00] — Introduction to the Laravel API course for beginners. Assumes viewers have basic Laravel knowledge and now want to build APIs.
- **No Need for a New Project** [0:21] — You don't need to create a new Laravel project to add APIs; you can use an existing one. If starting fresh, run `laravel new [project-name]`.
- **Using Starter Kits (Optional)** [0:43] — Starter kits like Laravel Breeze are optional but help with boilerplate (login, logout). Select 'blade with Alpine' as the stack; skip 'API only' since it will be covered later.
- **Project Setup & Database Choice** [1:40] — Choose SQLite for simplicity (no extra setup). Run default migrations. Open the project in a code editor.
- **No Default API.php in Newer Laravel Versions** [1:53] — Older courses referenced a default `api.php` route file, but it no longer exists in Laravel 9/10+. You must install API support manually.
- **Installing API Support with Artisan** [2:11] — Run `php artisan install:api` in the project root. This adds boilerplate code, a new migration (`create_personal_access_tokens`), and a new `routes/api.php` file.
- **Creating Your First API Route** [3:28] — In `routes/api.php`, define a route: `Route::get('/hello', function () { return response()->json(['message' => 'hello world']); });`.
- **Accessing API Routes with Prefix** [5:13] — API routes automatically get the `/api/` prefix. To access the route, use `/api/hello` in the browser. The prefix is customizable (covered in a later episode).
- **Why Use api.php Instead of web.php?** [6:00] — API requests are stateless (no sessions/cookies). Routes in `web.php` have CSRF protection enabled, which would break API POST requests. API routes bypass CSRF and are designed for stateless JSON responses.
- **Configuration in Bootstrap/app.php** [7:15] — Under `bootstrap/app.php`, Laravel defines the API routes and prefix. The prefix is customizable via the `api()` method or `withRouting()` configuration.

### Conclusion

This first episode sets up a Laravel project for APIs and demonstrates creating a simple JSON endpoint, preparing for more complex routes and database integration in the next episodes.

## Transcript

Hey everyone, welcome to the first
episode of my Laravel API course for
beginners. In this video series, I'm
going to be covering all the basic
information you need to know to start
building your own APIs inside Lavel. So
in this course, I'm assuming you guys
have the basics of Ludal down and now
you're trying to learn how to build your
own APIs. Okay, so let's get right into
it. So first thing first, you don't
actually need to create a new project to
start adding APIs. So if you already
have an existing project, you can follow
along. But if you don't, the first step
is for us to create a new project or a
new Laravel project. So I already have
the command here. So I'm going to go
ahead and run Laravel new. And this is
my project's name. You can name yours
whatever you like. Hit enter.
Now we don't necessarily need any
starter kits to add APIs, but just to
make things a bit easier, I'm going to
go ahead and select lot of a breeze. So,
we have some basic boiler plate, you
know, for logging, log out, uh, things
of that nature. If you're not familiar
with starter kits, you can skip this
part or just follow along as I'm doing.
And next up, for this one, I'm just
going to go blade with Alpine, the first
option. It doesn't really matter which
one you select. So, I'm just going to go
ahead with the first option. I do
recommend you skip the API only because
I'm going to be covering that again. So,
we're just going to go with the first
option. And I don't care about dark mode
for the course. I'm going to select
pest. And that's it. So now we should
have a new Lo project. If you already
have an existing one, you can skip this
part and follow on the next step.
All right. For database, you can select
any of these you like. I'm going to go
with skill light because it doesn't
require any setup. And I'm also going to
run the default migrations.
All right. Now, with the setup done,
let's go ahead and open up uh this
project in our code editor real quick.
Okay guys, so my project installation is
done. And I have also gone ahead and
opened the project in my editor. So
before we actually get started, you
might be looking at if you are looking
at some older courses online, uh you
might end up noticing that there is no
API. Here some some older courses I
think prior to Larl 9 or 10. There used
to be a default API. PHP that is no
longer here. So the first step we need
to do is actually to install API support
for Laravel. And for that we need to
actually run a command. So, open up your
terminal and make sure you have
navigated to your project root
directory, which is where I am right
now. And we need to run the following
artisan command. So, I'm going to run
PHP artisan
install API.
And what this command will do, it will
add some boiler plate code that allows
us to support APIs. It also add some
additional changes that I'm going to
cover real quick. It's also going to ask
you to publish some new database
migrations. I'm going to go ahead and
hit yes. Do that as well. So once you do
that, the first thing you notice is it
added a new database migration file. So
it's under database migrations. It's
this file. So it's going to be create
personal access tokens. Uh we're going
to be covering that on later episode.
This is for a lot of sanctum and it's
going to allow us to do authentication
and authorization for our APIs. So it's
quite powerful. Uh one more change that
it's going to also do is it's going to
add this new API.php
route file. So this one is very useful.
We're going to actually be defining all
our APIs inside this file. Okay, let's
get right into it. So now that we have
done the initial setup, it's actually
very easy to create our first API. It's
very similar to how you would do it with
normal web.php. PHP. All you have to do
is just simply do route
followed by the HTTP request you want.
So in this case, I'm going to go with a
get request.
Next up, you need to define your URL or
URI. In this case, we're going to be
building something super simple. For the
first episode, I'm going to say hello
for hello world. And last but not least
here, you can either pass in a
controller or for now, I'm going to keep
it super simple and I'm going to have an
inline function, basically an anonymous
function. So I'm going to say function
and I'm just going to define net over
here. Okay, on the next episode we got
are going to have a controller. So don't
worry about that. Now that we have
defined our route, usually if you have
an API, you want to return some sort of
JSON in your response. That's actually
very easy to do with Laravel. All we
have to do is do return response.
And on this response helper, there is a
JSON over here and this will allow you
to return JSON to your clients super
easily. Okay, so for the first parameter
is going to be what we are returning,
right? So that's going to be the
response of our API and this is going to
take an associative array in PHP and you
can return anything you want. So let's
say I want to return a message
and I'm going to return let's say hello
world. All right, just for now some
basic content returned. Don't forget the
semicolon.
And that's it. Okay, this is our first
super basic API. So, let's go ahead and
save this and open this up in a browser.
So, I have my project open over here.
Uh, now one thing you may notice if you
go ahead and do /hello,
you will actually get this 404 not
found, right? Even though if you look at
our route, it is actually called hello.
So uh what lable does is if you are
defining an API inside this API.php file
is it will always add an API prefix
before all your routes. So it will
always be API and then followed by
hello. So that's what actually we need
to put in. So I'm going to go back and
change it up to API hello.
And now you guys can see we are getting
actually a JSON response from our API.
Now a question you might have is like
hey why do we need to put our routes
inside this API.php and why can't I just
for example go ahead and put it inside
this web.php right what's the use case
is it just the prefix there's actually
more than that so usually if you have an
API it's going to be a stateless request
right while when you have this like
web.php PHP files it's going to be
stateful right so for example the user
might already be logged in they have
some sessions they have some cookies but
when they are using APIs they're not
going to have any cookies they're not
going to have any existing sessions so
it's going to be stateless and larville
is going to handle these two routes
separately also usually well actually
not usually always by default Laravel is
going to do CSRF authentication on
web.php PHP routes while with API routes
because it is stateless there is no CSRF
protection right so if you were for
example to put some sort of post request
or put request inside this web.php PHP
and you try to access it or call it
similar to like a normal API, Laravel is
going to be doing that CSRF protection
check and all your calls are going to
fail. Okay, so it's very important for
us to put all our APIs under this
API.php file to kind of keep it super
simple. Okay,
and one more change, I'll just quickly
show it to you guys. If you go actually
under your uh under your Bootstrap
app.php PHP file. You will also notice
that now there is this new line over
here. So this is actually where Laravel
is defining it. Right? So all the APIs
are going to be read under this routes
API.php file. Now later on on a later
episode I'm going to show you guys how
you can actually change this API prefix.
This is customizable. So none of none of
these are hardcoded. We can customize
them, but those are going to be for the
next episode. So that's it guys for
today's episode. Super basic. I just
wanted to cover how to get the initial
setup done and create your first API. On
the next episode, we're going to talk
about how to load data from the database
and also a bit more complex routes,
maybe having like dynamic URLs and
things of that nature. See you guys on
the next episode. Have a great day. Bye.
