TypeScript
---
title: "Part 2: Mount the HTTP API"
description: Serve Better Auth routes and an Effect HTTP API from the same Worker.
sidebar:
order: 2
---
Continue from [Part 1](/better-auth/tutorial/part-1). Keep Better Auth's routes public while building your application's API alongside them.
## Describe a health endpoint
```typescript
// src/api.ts
import * as Schema from "effect/Schema";
import * as HttpApi from "effect/http-api/HttpApi";
import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint";
import * as HttpApiGroup from "effect/http-api/HttpApiGroup";
export class PublicApi extends HttpApiGroup.make("public").add(
HttpApiEndpoint.get("health", "/api/health", {
success: Schema.Struct({ ok: Schema.Boolean }),
}),
) {}
export class AppApi extends HttpApi.make("app").add(PublicApi) {}
```
The schema declares the response shape. No authentication is required for this endpoint.
## Implement the endpoint
```typescript
// src/http.ts
import * as Http from "alchemy/Http";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import { AppApi } from "./api.ts";
const PublicLive = HttpApiBuilder.group(AppApi, "public", (handlers) =>
handlers.handle("health", () => Effect.succeed({ ok: true })),
);
export const HttpLive = HttpApiBuilder.layer(AppApi).pipe(
Layer.provide(PublicLive),
Layer.provide(Http.Platform),
);
```
The handler supplies the declared response. Alchemy's HTTP platform layer provides the services needed to build the API on a Worker.
## Build the router
```diff lang="typescript"
// src/worker.ts
+import * as HttpRouter from "effect/http/HttpRouter";
+import { HttpLive } from "./http.ts";
Effect.gen(function* () {
yield* Auth;
- return { fetch: Effect.succeed(HttpServerResponse.text("Ready")) };
+ const fetch = yield* HttpRouter.toHttpEffect(HttpLive);
+ return { fetch };
```
Build the router once during Worker construction. Remove the now-unused `HttpServerResponse` import.
## Mount Better Auth
```diff lang="typescript"
// src/worker.ts
+import { HttpServerRequest } from "effect/http/HttpServerRequest";
Effect.gen(function* () {
- yield* Auth;
+ const auth = yield* Auth;
const fetch = yield* HttpRouter.toHttpEffect(HttpLive);
- return { fetch };
+ return {
+ fetch: Effect.gen(function* () {
+ const request = yield* HttpServerRequest;
+ const pathname = request.url.split("?")[0];
+ if (pathname === "/api/auth" || pathname.startsWith("/api/auth/")) {
+ return yield* auth.fetch;
+ }
+ return yield* fetch;
+ }),
+ };
```
Better Auth handles its own endpoints and response cookies. Other paths go through the Effect HTTP API.
## Check the public API
```sh
curl "$DEV_URL/api/health"
```
```json
{"ok":true}
```
## Check an anonymous session
```sh
curl "$DEV_URL/api/auth/get-session"
```
```json
null
```
An anonymous request has no session. A database or authentication failure is an error, not an anonymous session.
Continue to [Part 3: Build sign-in](/better-auth/tutorial/part-3).