---
title: "API Call"
description: "How to create APIs with LaunchFa.st?"
source: "https://www.launchfa.st/documentation/tutorials/api-call"
---

> **Site index for agents**
> Fetch https://www.launchfa.st/llms.txt to discover every page on this site.
> Every page is also available as Markdown by appending `.md` to its URL.

import { Tabs, TabItem } from '@astrojs/starlight/components'

<Tabs>

<TabItem label="Astro">In Launchfa.st, to make it more easier for the developers, all the APIs are right now inside the `src/pages/api` directory.</TabItem>

<TabItem label="SvelteKit">In Launchfa.st, to make it more easier for the developers, all the APIs are right now inside the `src/routes/api` directory.</TabItem>

</Tabs>

<Tabs>

<div style="height: 1px; background: #C1C1C150; width: 100%;" />

<TabItem label="Astro">
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.

</TabItem>

<TabItem label="SvelteKit">
Any file inside the `src/routes` ending with `+server.ts` can be an API enpoint.

Find more on the API Routes in SvelteKit at https://kit.svelte.dev/docs/routing#server.

</TabItem>

</Tabs>

<div style="height: 1px; background: #C1C1C150; width: 100%;" />

## 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:

<Tabs>

<TabItem label="Astro">

1. Create a file: `src/pages/api/test.js`.
2. Add the content below to the `test.js`:

```typescript
// Filename: 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,
  })
}
```

</TabItem>

<TabItem label="SvelteKit">

1. Create a file: `src/route/api/test/+server.ts`.
2. Add the content below to the `+server.ts`:

```typescript
import { json, error } from '@sveltejs/kit'
import type { RequestEvent } from './$types'
import { getSession } from '@/lib/utils/auth'

// Handle GET Request to /api/test
export async function GET(event: RequestEvent) {
  // Get the user session from the 'request'
  const user = getSession(event.request)
  if (!user) {
    throw error(403)
  }
  // Extract the 'file' parameter from the request URL and do something
  const url = new URL(event.request.url)
  const file = url.searchParams.get('file')
}

// Handle POST Request to /api/test
export async function POST(event: RequestEvent) {
  // Get the user session from the 'request'
  const user = getSession(event.request)
  if (!user) {
    throw error(403)
  }
  if (user.email) {
    const data = await event.request.formData()
    // Get the 'file' field from the form data
    const file = data.get('file')
  }
}
```

</TabItem>

</Tabs>

<div style="height: 1px; background: #C1C1C150; width: 100%;" />
