# Connect Node.js fetch to SAM.gov data

To call SAM.gov data from Node.js, use the built-in fetch with headers { Authorization: `Bearer ${process.env.SAMGOVAPI_API_KEY}` } against https://samgovapi.io/api/v1 endpoints such as /registration-status/<UEI> or /opportunities. Responses are JSON with data and meta; pass meta.next_cursor as cursor for the next page.

Node's built-in fetch needs no packages, so the same code runs in a script, a serverless function or a queue worker. Keep calls on the server: a key in browser code is a key anyone can copy.

- Connects with: REST
- MCP server: https://samgovapi.io/api/mcp
- REST base: https://samgovapi.io/api/v1
- Auth: Authorization: Bearer <key>, kept in SAMGOVAPI_API_KEY

## Get a key

Free key at https://samgovapi.io/sign-up, or have the agent call start_signup and approve the link it shows you. Read process.env.SAMGOVAPI_API_KEY on the server only.

## Set up Node.js fetch

1. Set the key

Shell:

```bash
export SAMGOVAPI_API_KEY="sg_..."
```

2. Look up one registration

check.mjs:

```js
const r = await fetch("https://samgovapi.io/api/v1/registration-status/ZXBCU6K5KL99", {
  headers: { Authorization: `Bearer ${process.env.SAMGOVAPI_API_KEY}`, "X-User-Request": "check partner registration" },
});
if (!r.ok) throw new Error(`HTTP ${r.status}: ${await r.text()}`);
const { data } = await r.json();
console.log(data.status, data.expiration_date, data.renewal_window, "as of", data.as_of);
```

3. Page through a search

search.mjs:

```js
const H = { Authorization: `Bearer ${process.env.SAMGOVAPI_API_KEY}` };
let cursor = null;
do {
  const u = new URL("https://samgovapi.io/api/v1/opportunities");
  u.search = new URLSearchParams({ naics: "541512", open: "true", limit: "100", ...(cursor ? { cursor } : {}) });
  const body = await (await fetch(u, { headers: H })).json();
  for (const o of body.data) console.log(o.notice_id, o.response_deadline, o.title);
  cursor = body.meta.next_cursor;
} while (cursor);
```

## Check that it works

Expected output:

```
active 2027-10-20 current as of 2026-11-01
```

## Things to know

- Node 18 or later has fetch built in.

## Troubleshooting

- TypeError: fetch is not defined: Upgrade to Node 18 or later.
- 401 INVALID_API_KEY: The variable is not set in the process environment.
- 429: Back off until reset_at; the body names the limit.

## Questions

### Can I generate TypeScript types?

Yes. Point an OpenAPI generator at /openapi.json.

## Official documentation

- Node.js: fetch: https://nodejs.org/api/globals.html#fetch

Checked 2026-11-13. All guides: https://samgovapi.io/agents.md

Independent. Not affiliated with, endorsed by or sponsored by GSA, SAM.gov or any U.S. government agency. Registering in SAM.gov is free at sam.gov.
