# hazo_api Setup Checklist

Step-by-step setup for adding `hazo_api` to a consuming Next.js app.

## 1. Install

```bash
npm install hazo_api zod hazo_logs hazo_config
# Optional:
npm install hazo_debug
```

## 2. Configure Tailwind v4 (if using styles in the consuming app)

Add this to your `globals.css`:

```css
@import "tailwindcss";
@source "../node_modules/hazo_api/dist";
```

## 3. Add INI config

Copy the sample to your app:

```bash
mkdir -p config
cp node_modules/hazo_api/config/hazo_api_config.ini.sample config/hazo_api_config.ini
```

Edit values as needed. See `README.md` for option details.

## 4. Wrap your route handlers

Replace `NextResponse.json(...)` calls with `ok(...)` / `fail(...)`:

```typescript
import { withRequestContext, ok, fail } from 'hazo_api';

export const POST = withRequestContext(async (req) => {
  // ...
  return ok(data);
});
```

## 5. Add Swagger UI route

```typescript
// app/api/v1/docs/route.ts
import { generateOpenAPI } from 'hazo_api';
export const GET = () => Response.json(generateOpenAPI({ /* ... */ }));

// app/api/v1/docs/ui/route.ts
import { swaggerUiHtml } from 'hazo_api/client';
export const GET = () =>
  new Response(swaggerUiHtml({ spec_url: '/api/v1/docs' }), {
    headers: { 'Content-Type': 'text/html' },
  });
```

## 6. Verify

```bash
curl http://localhost:3000/api/v1/docs | jq .openapi
# Expected: "3.1.0"
```

Open `http://localhost:3000/api/v1/docs/ui` in a browser — Swagger UI should load.

## 7. (Optional) Enable debug panel

If you have `hazo_debug` installed, set in INI:

```ini
[debug]
enabled = true
```

Every request will be pushed to `hazo_debug`'s `RequestLogEntry` stream.

## v1.1.0 setup additions

1. Install the new peer deps in your consuming app:
   ```bash
   npm install hazo_connect hazo_ui   # now required
   npm install hazo_auth              # optional, only if using withApiKey({bind_user:true})
   ```

2. Run the migrations against your DB. The SQL files ship in `node_modules/hazo_api/migrations/`:
   - `001_hazo_api_keys.sql`
   - `002_hazo_api_rate_limits.sql`

   The Postgres variant is active; the SQLite variant is documented in comments — copy/paste if you're on SQLite.

3. Construct services once at app boot:
   ```typescript
   import { createApiKeyService, createRateLimitService } from 'hazo_api';
   export const apiKeys   = createApiKeyService({   getHazoConnect: () => getYourAdapter() });
   export const rateLimit = createRateLimitService({ getHazoConnect: () => getYourAdapter() });
   ```

4. Mount the admin routes (wrap them with your own auth — they ship unauthenticated):
   ```typescript
   // app/api/admin/keys/route.ts
   import { withRequestContext, createApiKeyRoutes } from 'hazo_api';
   const routes = createApiKeyRoutes({ service: apiKeys });
   export const GET  = withRequestContext(routes.list);
   export const POST = withRequestContext(routes.create);

   // app/api/admin/keys/[id]/route.ts
   export const DELETE = withRequestContext(
     async (req, { params }) => routes.revoke(req, { params: await params })
   );
   export const PATCH = withRequestContext(
     async (req, { params }) => routes.update(req, { params: await params })
   );
   ```

5. (UI) Drop `<ApiKeyManager api_base="/api/admin/keys" />` from `hazo_api/client` onto an admin page.

   For end-user-facing admin pages, pass `user_id` from your session and a `scopes_field` config for friendlier permission selection — see the **Customizing the scopes input** section in `README.md`:
   ```tsx
   <ApiKeyManager
     api_base="/api/admin/keys"
     user_id={session.user.id}
     scopes_field={{ mode: 'catalog', options: [/* ... */] }}
   />
   ```

6. Update `config/hazo_api_config.ini` with the new `[auth]` and `[rate_limit]` sections — see the sample.
