Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Member-only story

How to test Laravel with Sanctum API using the Postman

Balaji Dharma
Dev Genius
Published in
5 min readFeb 5, 2023

--

Photo by Benoit Debaix on Unsplash

In the last part, we completed the Laravel Breeze API installation and validated the API using the Breeze Next front end.

In this blog, we going to test the API using the Postman application.

About Postman

Postman is software used to test the API by sending and receiving the request with multiple data formats along with auth.

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs — faster.

Install Postman

Click here and complete your Postman installation. After installation opens the Postman application.

Create a new Postman Collection

Click the create new button

In the popup window click the collections

Enter the name “Laravel Admin API” and select auth type that isNo auth.

Pre-request Script

In the Laravel Sanctum, we used SPA authentication. So it works by using Laravel’s built-in cookie-based session authentication services. So, we need to set the cookie for all the requests in Postman.

We can set the cookie by using the Postman Pre-request Script. Add the below code to Pre-request Script.

pm.sendRequest({
url: pm.collectionVariables.get('base_url')+'/sanctum/csrf-cookie',
method: 'GET'
}, function (error, response, {cookies}) {
if (!error){
pm.collectionVariables.set('xsrf-cookie', cookies.get('XSRF-TOKEN'))…

--

--

Write a response