Connect to Your Own API

Before you start

This guide is for an app that calls an API you run somewhere else. moonbase runs the app; your API keeps running where it is.

moonbase has its own managed PostgreSQL database too. It is in a closed pilot: customers can't create one yet, and there is no date for wider availability. When a mission has one, its status is in the Database section of the mission's page, and the owner can export it, see Export Your Database. Until then, connect the outside service you already use, as below.

Which value goes in which variable

• The API's address: VITE_API_URL (Vite) or NEXT_PUBLIC_API_URL (Next.js) when browser code calls it, or API_URL when only your server code does. Public. It must be a public https address. A localhost address in your code works on your computer only.

• An API key or token: API_KEY (or the name your code reads). Server-only, without a public prefix. Anything the browser can read, a visitor can read too.

Where to enter them

Open your mission in Mission Control and go to Environment Variables. Type each value there. Values are write-only: after a save the screen shows only the name. Secrets go on that screen only, never in a chat with an AI assistant and never in your code or your ZIP.

An assistant connected to moonbase can save the public values for you. For a secret, it sends you to that same screen and you type it yourself.

A change applies on the next deploy: use Save and redeploy, or Deploy new version.

How the values reach your app

How a value reaches your app depends on what kind of mission it is:

• A mission served as it is (an index.html at the root, no build step): moonbase passes it no environment variables at all. Every file in it is public, so it can only hold public values, written straight into the page.

• A front end moonbase builds (Vite, Vue, Create React App, Astro, SvelteKit, Next.js): during the build, moonbase gives the build only the variables that carry the framework's public prefix: NEXT_PUBLIC_ for Next.js, VITE_ for Vite and Vue, REACT_APP_ for Create React App, PUBLIC_ for Astro and SvelteKit. The framework writes those into the files the browser downloads. A value without the prefix never reaches the build.

• Server code (a Next.js server, Express, Nuxt, SvelteKit with the Node adapter, Python): every saved variable is in the environment when the app starts (process.env in Node, os.environ in Python). Server-only secrets belong here, without a public prefix.

The build can download packages but can't reach other services. Load data when a visitor opens the page, not while the app is being built.

What your API has to allow

• Reachable from the internet. A mission can connect to public addresses on any port except outgoing mail on port 25. Private and internal addresses (10.x, 172.16-31.x, 192.168.x, loopback, 100.64.x and 169.254.x) are blocked, so an API on a private network won't answer.

• If browser code calls your API directly, the API has to allow your mission's live address (and your custom domain) as a CORS origin. Calls from your mission's server code don't need CORS.

Example: a mission with no build step

Only for an API that needs no secret, because every file in this mission is public.

<script>
  fetch("https://api.example.com/items")
    .then((r) => r.json())
    .then((items) => { document.body.textContent = JSON.stringify(items); });
</script>

Example: an Express server that keeps the key

Set API_URL and API_KEY on the Environment Variables screen. The browser calls your mission's own /api/items, and the server adds the key, so the key never reaches the browser.

// server.js (package.json: "start": "node server.js")
const express = require("express");

const app = express();
app.use(express.static("public"));

app.get("/api/items", async (req, res) => {
  const upstream = await fetch(process.env.API_URL + "/items", {
    headers: { Authorization: "Bearer " + process.env.API_KEY },
  });
  res.status(upstream.status).json(await upstream.json());
});

app.listen(process.env.PORT || 3000);

In a Next.js app the same call goes in a route handler (app/api/items/route.js) that reads process.env.API_KEY. A Vite app has no server of its own on moonbase, so it can only call an API that needs no secret.