Next.js ships with API routes which provides an easy solution to build your own API.
This example shows how to create an API endpoint with CORS headers, using the cors package.
Deploy the example using Vercel or preview live with StackBlitz
Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:
npx create-next-app --example api-routes-cors api-routes-cors-appyarn create next-app --example api-routes-cors api-routes-cors-apppnpm create next-app --example api-routes-cors api-routes-cors-appDeploy it to the cloud with Vercel (Documentation).