Server & API

Data Fetching

Patterns for fetching and managing data in Manic applications.

Data Fetching

Manic provides a flexible environment for data fetching. Because it is a fullstack framework, you can fetch data directly from your API routes or connect to external services.

Client-Side Fetching

The most common pattern in Manic is fetching data on the client using standard React hooks or libraries like react-query and swr.

import React, { ,  } from 'react';

export default function () {
  const [, ] = <string[]>([]);

  (() => {
    ('/api/users')
      .( => .())
      .( => (( as { : string[] }).));
  }, []);

  return (
    <>
      {.( => < ={}>{}</>)}
    </>
  );
}

Environment Variables

To keep your application secure, Manic distinguishes between Server-Only and Client-Accessible environment variables.

Public Variables

Any variable prefixed with MANIC_PUBLIC_ will be automatically injected into your client-side bundle by the OXC transformer.

.env
DATABASE_URL="postgres://..." # Server-only
MANIC_PUBLIC_API_URL="https://api.example.com" # Client-accessible

Accessing Variables

Use the getEnv helper from manicjs/env:

import {  } from 'manicjs/env';

const  = ('MANIC_PUBLIC_API_URL');

Server-Side Fetching

Inside an API route you have full server access — process.env, Bun.file, third-party SDKs, your database driver, etc. Environment variables from .env and .env.local are loaded automatically when the server starts:

app/api/posts/index.ts
import { Hono } from 'hono';

const app = new Hono();

app.get('/', async () => {
  const dbUrl = process.env.DATABASE_URL!;
  const posts = await fetch(`${dbUrl}/posts`).then(r => r.json());
  return Response.json({ posts });
});

export default app;

If you need to read environment values outside the request lifecycle (e.g. in a script), read from process.env after your runtime has loaded .env files:

const  = ..;

Type-Safe API Calls

Use Hono's client for type-safe API calls from the browser:

type  = {
  : {
    : () => <{ : () => <{ : string[] }> }>;
  };
};

declare function <>(: string): ;

const  = <>('/api');
const  = await ..();
const  = await .();

See Also

On this page