> ## Documentation Index
> Fetch the complete documentation index at: https://docs.keyport.sbs/llms.txt
> Use this file to discover all available pages before exploring further.

# Express

> Protect Express routes with the official KeyPort middleware package.

## Installation

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install keyport-express
```

## Setup and usage

<Steps>
  <Step title="Import the middleware">
    Import `keyportLicense` from `keyport-express` alongside your Express app:

    ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
    import express from 'express';
    import { keyportLicense } from 'keyport-express';

    const app = express();
    ```
  </Step>

  <Step title="Apply the middleware to a route group">
    Mount `keyportLicense` with `app.use` to protect any path. Supply your API key directly or point to a config file:

    <CodeGroup>
      ```typescript API key in code theme={"theme":{"light":"github-light","dark":"github-dark"}}
      app.use('/api', keyportLicense({
        apiKey: process.env.KEYPORT_API_KEY!,
      }));
      ```

      ```typescript Config file theme={"theme":{"light":"github-light","dark":"github-dark"}}
      app.use('/api', keyportLicense({
        configPath: './keyport.json',
      }));
      ```
    </CodeGroup>

    All routes mounted under the path you pass to `app.use` will require a valid license key before the request is forwarded.
  </Step>
</Steps>

<Note>
  The middleware validates the incoming license key against `POST /api/v1/validate` on each request. Requests that fail validation receive an error response before reaching your route handlers.
</Note>

<Tip>
  Load your API key from an environment variable (`process.env.KEYPORT_API_KEY`) rather than embedding it in source code.
</Tip>
