# API

### Structure

All your "backend" stuff is located in the `/api` folder.

Any `route.ts` file will be an API endpoint, and the folder route will be the final API route. You can define dynamic content as well:

| folder                       | url                        |
| ---------------------------- | -------------------------- |
| /api/products                | /products                  |
| /api/products/newest         | /products/newest           |
| /api/products/\[slug]        | /products/the-best-product |
| /api/category/\[id]/products | /category/1337/products    |

### Calls

API calls are handled by [Tanstack-Query](https://tanstack.com/query/v3), a powerful and simple library that handle cache, background updates or stale data out of the box.

* GET example:

{% code lineNumbers="true" %}

```typescript
const { isLoading: isUsersLoading, data: users } = useQuery({
    queryKey: [`users`],
    queryFn: async () => {
      const res = await fetch(`/api/users`);
      return res.json();
    },
  });
```

{% endcode %}

* POST example:

{% code lineNumbers="true" %}

```typescript
const createMutation = useMutation({
    mutationFn: async (values) => {
      const res = await fetch("/api/categories", {
        method: "POST",
        body: JSON.stringify({
          ...values,
        }),
      });
      return res.json();
    },
  });
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.directoryfa.st/features/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
