This content originally appeared on DEV Community and was authored by Bright Agyemang
Okay hear me out…
Building Laravel SPAs with Sanctum shouldn’t feel like a whole ritual every time:
- Get CSRF cookie
- Dig up the
XSRF-TOKENcookie - Attach headers manually
- Pray the request doesn’t 419 💀
So I built a tiny fetch wrapper that handles all that automatically:
lara-fetch — a lightweight client that handles Sanctum csrf-cookies for you
(with zero stress + optional oversharing aka debug mode 😌)
💡 What Problem Does It Solve?
Instead of doing something like:
await fetch('/sanctum/csrf-cookie', { credentials: 'include' })
// then extract token from cookie
// then set headers
// then hope…
You can just:
import { laraFetch } from "lara-fetch"
laraFetch.post('/user/auth', {
body: JSON.stringify({ email, password })
})
Done ✅
Sanctum + CSRF ✅
No stress ✅
⚙️ One-Time Setup (Literally ONE line)
Add this in your main entry file:
import { laraFetch } from "lara-fetch"
laraFetch.configure({
baseURL: "http://localhost:8000", // Your Laravel API URL
debug: true // optional: console oversharing 😅,
... // additional config as per need
})
And BOOM 💥 — all requests automatically handle cookies + CSRF.
🧐 Why I’m Proud of This
✅ Zero boilerplate
✅ No more “Token mismatch” rage 😤
✅ Works with:
- Vue / Nuxt
- React / Next
- Even vanilla JavaScript 🫶
✅ Debug mode logs every move it makes 👀
📦 Installation
npm i lara-fetch
or
pnpm add lara-fetch
Yes, pnpm gang rise ✊🔥
🔗 GitHub Repo
Stars fuel open-source energy 🌟
👉 Check repo
PRs + feature ideas super welcome!
✅ Roadmap
- Pending: Auto session refresh
- Pending: Retry system
- Pending: Official docs + starter templates
✨ Final Word
I built lara-fetch because I was suffering…
and I refuse to let other developers suffer too 😌
If you try it and it slaps — drop a comment 💬 and star the repo 🌟
This content originally appeared on DEV Community and was authored by Bright Agyemang
Bright Agyemang | Sciencx (2025-10-29T04:40:16+00:00) 🚀 Introducing lara-fetch – Laravel Sanctum made SIMPLE (no tears 😭). Retrieved from https://www.scien.cx/2025/10/29/%f0%9f%9a%80-introducing-lara-fetch-laravel-sanctum-made-simple-no-tears-%f0%9f%98%ad/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.