[hypequery](https://hypequery.com/) › Internal Product APIs with ClickHouse Analytics | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/use-cases/internal-product-apis

> Add ClickHouse analytics to the backend you already run without rewriting your public API surface first.

Use Case

# Internal product APIs with embedded ClickHouse analytics

This is the migration path for teams that already have a backend and do not want to split it into a separate analytics service on day one. Keep the existing routes, move the query logic onto a shared path, and expose more only when you need it.

[Start with this pattern](https://hypequery.com/docs/quick-start)[Book architecture chat](https://cal.com/luke-reilly-jdi9su/hypequery-chat)

Contract impact

Zero endpoint breakage

Execution model

In-process + routed

Migration style

Endpoint-by-endpoint

## Keep route contracts stable

Existing consumers do not need to notice the migration while you move analytics logic behind an internal path.

## Use one query definition

Let handlers call \`api.run(...)\` directly and expose the same definition over HTTP only where it is genuinely useful.

## Scale rollout safely

Start with one endpoint or one response shape, see what sticks, then widen the internal surface deliberately.

## Implementation flow

Step 1

### Mount internal routes

Mount the analytics handler beside the routes you already run instead of restructuring the whole backend first.

Step 2

### Compose in endpoints

Pull analytics into existing business responses without turning every route into custom query code.

Step 3

### Reuse query logic

Keep one typed query definition whether the query is called directly or exposed under an internal path.

## Works with your existing routes

The route tree stays yours. Add the analytics surface where it fits instead of adopting a separate service boundary immediately.

routes.ts

```
import { createFetchHandler } from '@hypequery/serve';

const hypequeryHandler = createFetchHandler(api.handler);
app.route('/api/products', products);
app.all('/internal/hq/*', (c) => hypequeryHandler(c.req.raw));
```

## Compose analytics directly in business endpoints

Use \`api.run(...)\` when a product response needs analytics context but does not need another network hop inside the same backend.

products.ts

```
const productId = c.req.param('productId').toUpperCase();
const analytics = await api.run('productOverview', {
  productId,
});

return c.json({ productId, analytics });
```

## Define once, reuse everywhere

This adoption path depends on one definition backing both an internal route and an in-process call.

analytics/api.ts

```
const { query, serve } = initServe({
  context: () => ({ db }),
});

const topProducts = query({
  output: z.array(z.object({
    product_id: z.string(),
    avg_order_value: z.number(),
  })),
  query: ({ ctx }) =>
    ctx.db
      .table('analytics.orders')
      .select(['payment_type as product_id'])
      .count('trip_id', 'orders')
      .avg('order_total', 'avg_order_value')
      .groupBy(['payment_type'])
      .execute(),
});

export const api = serve({
  basePath: '/internal/hq',
  queries: { topProducts },
});
```

Ready to implement

## Use this as your first production migration path

- Keep existing consumer contracts intact
- Move the query logic into one typed definition
- Expose more surface area only when the team actually needs it

[Open quick start](https://hypequery.com/docs/quick-start)[Next: Multi-tenant](https://hypequery.com/use-cases/multi-tenant-saas)
