Add server functions and call an outside API with a secret
Save an API key as a secret, connect an outside service as an endpoint, and ask the AI builder to write a server function that your page calls. The key never reaches the page. About thirty minutes.
Goal: a page that shows data from an outside service, using Studio Functions and Studio Connect so that a secret key stays on the server side. Time: about thirty minutes. Plan: Studio. Before you start: an account with an outside service that offers an https API and an API key, for example a currency rates or weather service. We call it api.example.com; use the real address from your service's documentation.
1. Save the key as a secret
Open an app note, then its configuration (Configuration).
Under Environment variables choose Add a variable.
Name it
RATES_KEY, paste the key and switch on Secret. Save.
2. Add the endpoint
Under Endpoints choose Add an endpoint.
Name it
ratesand enter the Base URL your service documents, for examplehttps://api.example.com/v1. Use the narrowest base you can.Allow the GET method.
Add a header: name
Authorization, valueBearer {{RATES_KEY}}. If your service takes the key as a query parameter, add it under Query parameters instead, for examplekeyset to{{RATES_KEY}}.Save.
If you paste a real key in a header value, SeeMyNotes refuses it and tells you to use a secret variable.
3. Ask the builder for the server function
Send this to the AI builder:
Add a server function in server/api.js with a GET route /rate. It reads the query parameters from and to, checks both are three letters, calls the endpoint named rates for the exchange rate, and returns JSON with the rate. Return a 400 with a clear message for bad input and a 502 if the service fails. On the page, add two inputs and a button that call /_smn/api/rate and show the result.
The builder writes server/api.js, similar to this:
export async function GET(request) {
if (request.path !== '/rate') return Response.json({ error: 'Not found' }, { status: 404 });
const { from, to } = request.query;
if (!/^[A-Za-z]{3}$/.test(from || '') || !/^[A-Za-z]{3}$/.test(to || '')) {
return Response.json({ error: 'from and to must be three letters' }, { status: 400 });
}
const response = await smn.fetch('rates', 'latest?from=' + from + '&to=' + to);
if (!response.ok) return Response.json({ error: 'The rates service failed' }, { status: 502 });
return Response.json(await response.json());
}
The exact path and parameters depend on your service.
4. Try it
Open the preview, enter two currency codes and choose the button. The page calls your function; the function calls the service with your secret; you see the result. Open the page's network tab: the key is nowhere in the requests or responses.
5. Fix problems
If it fails, tell the builder: "The rate route returns 500. Read the server logs and fix it." It can read the logs of your preview runs. Server code in your preview shows errors in detail; on the live app only errors are kept.
6. Know the limits
A server function runs for a few seconds at most, has no packages, no fetch and no file system, and can call the outside only through smn.fetch. See Server functions.
Result
An app that uses an outside API safely: the key stays in SeeMyNotes, input is checked on the server, and your page only sees the result.