> For the complete documentation index, see [llms.txt](https://program.hackyourfuture.dk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://program.hackyourfuture.dk/course-content/backend/node/week3/session-materials/14-auth-api-keys.md).

# 14. Auth - API Keys

In this final part, you will look at **API keys** as a simple mechanism for **machine-to-machine authentication**, and you will review the authentication methods covered in the session.

## 1. API keys: concept

* API keys are usually long, random strings given to other services or scripts.
* The client includes the key with each request (for example in an `x-api-key` header).
* The server validates the key and decides whether to allow the request.
* API keys are convenient for machines, but not ideal as the only method of authenticating human users.

## 2. Adding an API-key-protected route (Snippets API)

As an example, you can:

* Introduce an environment variable like `API_KEY`.
* Create a middleware (e.g. `requireApiKey`) that:
  * Reads the `x-api-key` header.
  * Compares it with the configured key.
  * Returns `401` if it is missing or incorrect.
* Use this middleware on a “machine-style” endpoint, such as:
  * A `/metrics` or `/health` endpoint.
  * A bulk export route for snippets data.

## 3. Optional rate limiting idea

To prevent abuse, you can sketch (or implement) a very simple rate limiting strategy:

* Use an in-memory object to count requests per API key.
* Deny further requests after a certain number within a short time window.
* Discuss why a production-grade solution would need shared storage and more robust logic.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://program.hackyourfuture.dk/course-content/backend/node/week3/session-materials/14-auth-api-keys.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
