Zum Inhalt springen

API Call

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

In Launchfa.st, to make it more easier for the developers, all the APIs are right now inside the src/pages/api directory.

Any file inside the src/pages ending with .js/.ts can be an API enpoint.

The good thing with APIs with Launchfa.st is that you get to use the Web APIs on top of Astro. This ensures that the code can be reused in any number of projects irresepctive of their stack.

Find more on the API Routes in Astro at https://docs.astro.build/en/core-concepts/endpoints.

Protected API Routes

To illustrate, to create an API route that returns whether a user is logged in or not, follow the steps in the LaunchFa.st template:

  1. Create a file: src/pages/api/test.js.
  2. Add the content below to the test.js:
src/pages/api/test.js
// Handle GET Request to /api/test
export async function GET({ request }) {
// Get the user session from the 'request'
const session = getSession(request)
if (!session) {
return new Response('unauthenticated', {
status: 403,
})
}
return new Response('authenticated', {
status: 200,
})
}
// Handle POST Request to /api/test
export async function POST({ request }) {
// Parse the incoming form data from the 'request'
const context = await request.formData()
// Get the user session from the 'request'
const session = getSession(request)
if (!session) {
return new Response('unauthenticated', {
status: 403,
})
}
return new Response('authenticated', {
status: 200,
})
}