← Integrations

Mobile app · TypeScript

Expo Router API routes as the server for your astrology app

With Expo Router your server can live in the same project as the app: a file ending in +api.ts runs on the server, never in the bundle. This page uses one to turn onboarding birth data into a Lagna, Moon sign and nakshatra, and deploys it with EAS.

Updated

How this page was checked: app/api/birth-details+api.ts was executed unchanged on Node 24 against the in-memory API: the exported POST was called with a web Request, which is what Expo Router passes it. The 200 and both 400s below come from that run; the Unrecognized key message is from posting the same body, name included, straight to AstroNode. The screen code, app.json and the EAS commands were not run; there is no simulator or EAS project behind this page.

Where this fits

The React Native page covers the app side: a separate backend, why EXPO_PUBLIC_ values are readable from every install, and showing UTC times. The React Native chart app template is a complete app with its own proxy. This page is about the other option: no separate backend at all, because Expo Router can serve API routes from the same codebase.

app.json - server output, and where release builds send relative requests:

json
{
  "expo": {
    "web": { "output": "server" },
    "plugins": [["expo-router", { "origin": "https://your-app.expo.app" }]]
  }
}

The API route

app/api/birth-details+api.ts:

ts
const ASTRONODE = process.env.ASTRONODE_BASE_URL ?? "https://api.astronode.dev";
const FIELDS = ["date", "time", "tz_offset_minutes", "lat", "lng"] as const;

export async function POST(request: Request): Promise<Response> {
  const input = await request.json().catch(() => null);
  if (!input) return Response.json({ error: "Expected a JSON body" }, { status: 400 });

  // Onboarding forms collect a name too; the birth input rejects unknown keys.
  const birth = Object.fromEntries(FIELDS.map((k) => [k, input[k]]));
  const res = await fetch(`${ASTRONODE}/v1/birth-details`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.ASTRONODE_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(birth),
  });
  const json = await res.json();
  if (!res.ok) {
    const fields = (json.error.details ?? []).map((d: { instancePath: string }) => d.instancePath);
    return Response.json({ error: json.error.code, fields }, { status: res.status });
  }
  const d = json.data;
  return Response.json({
    lagna: `${d.ascendant.sign} ${d.ascendant.degree.toFixed(1)}°`,
    moonSign: d.moon_sign,
    nakshatra: `${d.nakshatra} (pada ${d.nakshatra_pada})`,
    siderealSunSign: d.sun_sign,
    weekday: d.weekday,
  });
}

Each exported function is named for an HTTP method, takes a standard Request and returns a Response. Other methods get 405 automatically. The route reads process.env.ASTRONODE_API_KEY directly: server code can see every environment variable, not only EXPO_PUBLIC_ ones, and the bundler strips the route and its imports from the app.

What the onboarding screen gets

Posted with a `name` field: 15 August 1990, 06:40, Bengaluru, +330
FieldValue
lagnaLeo 5.2°
moonSignTaurus
nakshatraRohini (pada 2)
siderealSunSignCancer
weekdayWednesday

Name the sun sign as sidereal on screen. Someone born on 15 August "is a Leo" in every Western horoscope, and here, because the Vedic endpoints use the Lahiri ayanamsa, the Sun is in Cancer. Without a label that looks like a bug report waiting to happen (ayanamsa). The route also drops the name the form sends: forwarded as-is, AstroNode rejected it with Unrecognized key: "name". A time typed as 6:40 AM came back as 400 with fields: ["/time"]; /v1/birth-details is on the Free plan.

The screen side:

ts
// In a screen: a relative URL. In development it goes to the dev server;
// in a release build, to the origin set in app.json.
const res = await fetch("/api/birth-details", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name, date, time, tz_offset_minutes, lat, lng }),
});
const profile = await res.json();

EAS: which variables go where

bash
# Server-side key for the API route: "sensitive", not "secret" (see below)
eas env:create --environment production --name ASTRONODE_API_KEY \
  --value aie_live_... --visibility sensitive

# Local .env for `npx expo start`, pulled from the same environment
eas env:pull --environment development

# Ship the server: export, then deploy with that environment's variables
npx expo export --platform web
eas deploy --environment production --prod
  • Use `sensitive`, not `secret`, for the route's key. EAS secret variables can only be read on Expo's build servers and cannot be deployed with EAS Hosting, so the API route would run without its key.
  • Visibility does not protect an `EXPO_PUBLIC_` variable. Marking EXPO_PUBLIC_ASTRONODE_API_KEY as secret only hides it in the dashboard. The prefix still inlines the value into the app and web bundles at export time.
  • Deployments are immutable. After rotating the key, export and deploy again; the running deployment keeps the value it was deployed with.

The mistakes that bite first

  • A release build that cannot reach `/api`. In development, relative requests go to the dev server, so everything works. A release build has no dev server: without origin in the expo-router plugin config, the onboarding request fails only after you ship. Set origin to the deployed host before you build.
  • A shared helper imported by a screen. Only the +api.ts file and what it imports are kept out of the bundle (this needs expo/metro-config in metro.config.js). If a screen imports the same astronode.ts helper to reuse a type, the helper and anything it reads are bundled into the app. Keep types in a file with no process.env in it.
  • The wrong output mode. API routes need a server bundle: web.output: "server", or "static" with the expo-router plugin's apiRoutes option if you also want build-time HTML. Then npx expo export writes the server next to the client.

Questions

Which AstroNode endpoints does the Expo example call?
1 endpoint: Birth Details (Rashi, Nakshatra, Lagna) (POST /v1/birth-details).
Was the Expo code on this page actually run?
app/api/birth-details+api.ts was executed unchanged on Node 24 against the in-memory API: the exported POST was called with a web Request, which is what Expo Router passes it. The 200 and both 400s below come from that run; the Unrecognized key message is from posting the same body, name included, straight to AstroNode. The screen code, app.json and the EAS commands were not run; there is no simulator or EAS project behind this page.