# Building with AI Most sites on this API are written with an AI tool: Claude, ChatGPT, Cursor, Lovable and others. These docs are made for that. This page gives you a prompt to paste, the files to hand your tool, the patterns it should produce, and what to check in what it wrote. ## What to give your tool | File | What it is | | --- | --- | | [`https://docs.apidata.one/llms-full.txt`](https://docs.apidata.one/llms-full.txt) | Every page of these docs in one text file. Give it this when the tool can read a long file | | [`https://docs.apidata.one/llms.txt`](https://docs.apidata.one/llms.txt) | An index: each page with one line about it and a link | | [`https://docs.apidata.one/openapi.json`](https://docs.apidata.one/openapi.json) | The API as an OpenAPI 3.1 document: every endpoint, parameter, answer and error | | Any page with `.md` in place of `.html` | That page as plain Markdown | Build with a **test key** first. The [sandbox](sandbox.md) answers exactly like live, costs nothing, and has a listing for every awkward case. ## The prompt Paste this before you describe the site you want. It states the rules a site on this API must keep. ```text You are building a real-estate search site on the apidata.one API (https://api.apidata.one/v1). Read https://docs.apidata.one/llms-full.txt before writing code, and follow these rules exactly. KEYS - There are two keys. The secret key (sk_...) stays on the server, in an environment variable. It never appears in browser code, in a page, in a NEXT_PUBLIC_ or VITE_ variable, or in git. - The publishable key (pk_...) is the only key allowed in browser code. Use it only for what must run in the browser: a moving map, a search box that completes, a favorite button. - Every request sends "Authorization: Bearer ". The key picks the environment: _test_ keys reach the sandbox, _live_ keys reach real listings. The host and the paths never change. PAGES - Build listing pages and search pages ON THE SERVER with the secret key, so that they work without scripts and search engines can read them. Do not build a browser-only app. - A listing's page address is built from its listingKey, and the listing is looked up by listingKey alone: /listing/{listingKey}/{words}. Take the words from the listing's "slug" field. Never build an address from the street address. - Treat listingKey as text, never as a number. A VISITOR'S OWN DATA (favorites, saved searches, saved shapes) - The API never trusts a visitor's identity sent from the browser. Give each signed-in visitor an opaque ID (a UUID; never an email address or a phone number). - On the server: the secret key plus the header "X-Client-Id: ". - In the browser: the publishable key plus "X-Client-Token", a short-lived token the server mints for that visitor with POST /v1/client-tokens. Add a server route that mints it for the signed-in visitor, and refresh it before it expires (15 minutes by default). - Send an "Idempotency-Key" header (a new UUID for each action) on every POST that saves something. WHAT EVERY PAGE WITH A LISTING MUST SHOW - Every answer with listings has a "compliance" object beside "data". Show its texts as given: copyright, accuracy and lastUpdated on every page with a listing; idxSource, personalUse and availability on the home page or every search page; advertisingBrokerage.name on every page with a listing. Never hard-code these texts: read them from the answer each time. - Every listing card shows attribution.listOfficeName. A listing's detail page shows the listing office's name with its phone or email in the top quarter of the page, in the same size and colour as the other details. - Closed listings show compliance.closed.label and both of its statements. Coming Soon listings show compliance.comingSoon.label. - Photos are shown whole: CSS "object-fit: contain", never "cover". Never crop, overlay, filter or otherwise alter a photo. Use the photo URLs the API returns, with a plain tag, and do not pass them through an image optimizer or a CDN that keeps its own copies. - When a listing has addressDisplay "withheld", its address is null. Show the listing's addressNotice instead, and never show, guess or rebuild the address anywhere: not in the page, its title, its URL, its meta tags, its structured data or on a map. - A field that is null or missing must not be shown and must not be computed (for example days on market or price history for a Coming Soon listing, or a hidden price). FRESHNESS AND REMOVAL - Do not store listings in your own database. Fetch them from the API when a page is built. A built page may be kept for a few minutes to answer quickly. Never show one that is more than 12 hours old. - Handle the notice "listing.removed" (by webhook, or by reading GET /v1/events every minute): delete every stored copy of the listings it names, and answer 410 Gone for their pages. - A 404 with error.code "listing_not_found" means the listing is gone: show a "no longer available" page. - A 500 with error.code "data_stale" means live data is temporarily stopped: show "temporarily unavailable" and never a stored copy. ERRORS AND LIMITS - Every error is JSON: { "error": { "code", "message", "fix", "docs", "requestId" } }. Decide by error.code, never by the message text. Log requestId. - 429 "rate_limited" and 500 "temporarily_unavailable" or "data_stale" carry a Retry-After header: wait that long, then retry. Most HTTP libraries do not retry a 500 by themselves. - An HTML page with status 502, 503 or 504 is not from the API: retry after a few seconds. - In the browser, call the API once when the visitor stops typing or stops moving the map (wait about 250 ms), and cancel the call that is no longer needed. TEST AGAINST THE SANDBOX - Use the test keys. Check these fixed listings: SBX900000001 (hidden address), SBX900000003 (Coming Soon, no photos), SBX900000004 (Closed), SBX900000008 (prices not shown), SBX900000009 (150 photos), SBX900000024 (not served: 404). ``` ## The patterns, in Fastify A plain Node server that builds its pages. Nothing here needs a build step. **A listing page, built on the server:** ```js import Fastify from 'fastify'; const API = 'https://api.apidata.one/v1'; const secret = { Authorization: `Bearer ${process.env.APIDATA_SECRET_KEY}` }; // server only const app = Fastify(); app.get('/listing/:listingKey/:words?', async (request, reply) => { const r = await fetch(`${API}/listings/${encodeURIComponent(request.params.listingKey)}`, { headers: secret }); if (r.status === 404) return reply.code(404).type('text/html').send(notAvailablePage()); if (!r.ok) { return reply.code(503).header('Retry-After', r.headers.get('retry-after') ?? '300') .type('text/html').send(unavailablePage()); } const { data: listing, compliance } = await r.json(); return reply.type('text/html').send(listingPage(listing, compliance)); // escape every value }); ``` **A client token for the signed-in visitor**, so that the browser can save a favorite with the publishable key: ```js app.post('/api/client-token', async (request, reply) => { const visitor = await signedInVisitor(request); // your own sign-in if (!visitor) return reply.code(401).send(); const r = await fetch(`${API}/client-tokens`, { method: 'POST', headers: { ...secret, 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: visitor.opaqueId }), // a UUID of yours, never an email address }); if (!r.ok) return reply.code(502).send(); return (await r.json()).data; // { token, expiresAt } }); ``` ```js // In the browser: the publishable key and the visitor's token. async function saveFavorite(listingKey) { const { token } = await (await fetch('/api/client-token', { method: 'POST' })).json(); return fetch('https://api.apidata.one/v1/favorites', { method: 'POST', headers: { Authorization: 'Bearer pk_test_…', 'X-Client-Token': token, 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID(), }, body: JSON.stringify({ listingKey }), }); } ``` ## The same patterns, in Next.js Many tools write Next.js. The rules are the same; these are the places it goes wrong. **A listing page is a server component.** It runs on the server only, so the secret key is safe there: ```jsx // app/listing/[listingKey]/[[...words]]/page.jsx import { notFound } from 'next/navigation'; export const dynamic = 'force-dynamic'; // build from the API on each request: no stored copy export default async function ListingPage({ params }) { const { listingKey } = await params; const r = await fetch(`https://api.apidata.one/v1/listings/${encodeURIComponent(listingKey)}`, { headers: { Authorization: `Bearer ${process.env.APIDATA_SECRET_KEY}` }, cache: 'no-store', }); if (r.status === 404) notFound(); if (!r.ok) throw new Error('listings are temporarily unavailable'); // shown by error.jsx const { data: listing, compliance } = await r.json(); return (

{listing.address ? listing.address.line1 : listing.addressNotice}

Listed by {listing.attribution.listOfficeName} {listing.attribution.listOfficePhone ? `, ${listing.attribution.listOfficePhone}` : ''}

{listing.photos.items.map((photo) => ( // A plain : the API's own URL, shown whole. ))}

{compliance.advertisingBrokerage.name}

{compliance.copyright} {compliance.accuracy}

{compliance.lastUpdated}

); } ``` **The client token comes from a route handler:** ```js // app/api/client-token/route.js import { NextResponse } from 'next/server'; export async function POST(request) { const visitor = await signedInVisitor(request); // your own sign-in if (!visitor) return new NextResponse(null, { status: 401 }); const r = await fetch('https://api.apidata.one/v1/client-tokens', { method: 'POST', headers: { Authorization: `Bearer ${process.env.APIDATA_SECRET_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: visitor.opaqueId }), cache: 'no-store', }); if (!r.ok) return new NextResponse(null, { status: 502 }); return NextResponse.json((await r.json()).data, { headers: { 'Cache-Control': 'no-store' } }); } ``` Where Next.js sites go wrong: - **The secret key in a `NEXT_PUBLIC_` variable, or in a file marked `'use client'`.** Either one ships it to every visitor. The API refuses a secret key that arrives from a browser ([`403 secret_key_in_browser`](../errors/secret_key_in_browser.md)) and flags it: revoke that key and make another. - **Pages kept for a day.** Static generation and long `revalidate` times keep copies of listings. Build listing pages on each request, or keep them for minutes, and drop them when `listing.removed` arrives. - **`next/image` with `fill` and the `cover` fit.** That crops photos. Use a plain `` with `object-fit: contain`, or `next/image` with `unoptimized`: the API already gives five sizes, and an image optimizer keeps its own copies of photos. - **A `fetch` from a client component straight to the API with the secret key.** Client components call the API with the publishable key, or call your own route handlers. ## Check what the tool wrote - [ ] Search the code for `sk_`: it appears only where the server reads an environment variable - [ ] View a listing page's source in the browser: the listing is in the HTML, and no secret key is - [ ] `SBX900000001`: no address anywhere on the page, in its title or in its address - [ ] `SBX900000003`: a Coming Soon label, no photo, no days on market, nothing broken - [ ] `SBX900000004`: the Closed label and both statements - [ ] `SBX900000024`: a "no longer available" page, not an error page - [ ] The disclosures on every page with a listing, read from `compliance` - [ ] Photos whole on a narrow screen and a wide one - [ ] A handler for `listing.removed`, and `410 Gone` afterwards The [compliance guide](compliance.md) and [an SEO-friendly site](seo.md) explain each rule, and the sample site at [demo.apidata.one](https://demo.apidata.one) follows all of them.