Is there a way to use fetch api from inside api route in NextJs? I have quite a big handler and instead of doing everything in one handler I would like to make it more modular so that after doing a job that is made for (file name el. write-to-db) I would like to call another handler eg (send-emails). Is it possible in NextJS?
Thanks!
Is there a way to use fetch api from inside api route in NextJs? I have quite a big handler and instead of doing everything in one handler I would like to make it more modular so that after doing a job that is made for (file name el. write-to-db) I would like to call another handler eg (send-emails). Is it possible in NextJS?
Thanks!
Share Improve this question asked Jun 27, 2022 at 14:30 sevenseven 1,7043 gold badges23 silver badges53 bronze badges 3- 1 Why don't you just put everything into different functions (and/or files) and call them sequentially? You shouldn't be calling internal API routes for other API routes. – juliomalves Commented Jun 28, 2022 at 18:29
- 2 reasons are caching and consistency of code. Also that API call might live somewhere else one day, and ideally that would mean just changing the url – Adam Tolley Commented Mar 3, 2023 at 10:37
- @juliomalves correct answer – Markus Kottländer Commented May 3, 2024 at 4:55
1 Answer
Reset to default 10const baseUrl = process.env.VERCEL_URL ? 'https://' + process.env.VERCEL_URL : 'http://localhost:3000'
await fetch(`${baseUrl}/api/[your-route-name-here]`)