Skip to content

Local search · English

    Tutorial: connect API data to reusable components

    Build a reusable product list using an external JSON API. The component will have a configurable heading and a repeater with typed product fields. You will test zero and false values, publish the layout, then verify what happens when the source changes or becomes unavailable.

    Strukt synchronizes API data into a read-only collection. Components resolve that normalized data; opening the public page does not make a new upstream request. This walkthrough uses a REST JSON read endpoint, not arbitrary scripts or write requests.

    • Use a sample website with permission to configure Sources, edit components and publish.
    • Prepare a public HTTPS API you control or are authorized to read. The addresses below use reserved example domains and will not serve a real response; replace them with your own endpoint and image URLs.
    • Return only content intended for public pages. Source credentials are private connection settings, but imported record values can appear in Design and on public pages. Upstream drafts are not a private content channel.
    • Use stable, unique record IDs. Start with two records and no pagination; configure supported paging if your actual API requires it.

    Assume GET https://api.example.test/v1/products returns this top-level array:

    [
    {
    "id": "product-a",
    "title": "Alpha",
    "price": 0,
    "enabled": false,
    "day": "2026-11-02",
    "at": "2026-11-02T12:34:56+08:00",
    "photo": "https://images.example.test/alpha.png"
    },
    {
    "id": "product-b",
    "title": "Beta",
    "price": 2,
    "enabled": true,
    "day": "2026-11-03",
    "at": "2026-11-03T00:00:00Z",
    "photo": "https://images.example.test/beta.png"
    }
    ]

    The 0 and false values are deliberate. They must remain real values throughout mapping, preview and publication.

    Open Sources → Connect source. Set Name to Product API and Base URL to https://api.example.test/v1 using your actual API address.

    Choose None only for an endpoint that needs no credentials. Otherwise select Bearer token, API key header or Basic, and enter the required credential in the connection form. Keep the credential out of page HTML, component properties and example URLs. Choose Poll for changes, then select Save and continue. Complete the webhook-secret acknowledgement shown before the endpoint form; a manual Refresh now remains available even if polling is off.

    Set Collection name to Products, Slug to products, and Path to /products. Leave Records path empty for the top-level array above. If your response is wrapped as {"data":{"items":[...]}}, use data.items instead. Select Test request and confirm two records before Next: mapping.

    Set Record id path to id and Title path to title. Select the six fields below from Fields from the first record, then review their keys and types:

    API path Collection field key Type
    title title text
    price price number
    enabled enabled boolean
    day day date
    at at datetime
    photo photo media

    Do not rely on the first sample to infer temporal or media types. Explicitly choose date, datetime and media here. Select Save and refresh, then Open collection. Check Live snapshot, record count and Latest stored snapshot. A tested response is only a sample; the refresh checks the fetched records. A held or failed snapshot does not replace the live one.

    Create a section named Product list from a selected container, then open Edit selected section. Add a heading and a list with one product item and an empty-state sibling.

    Expose the heading as a text property named heading using Attributes → Reusable section fields → Create and connect text property. On the list container, use Repeater property to create items with the same six field names/types as the table. Connect the item controls to strukt.item.title, strukt.item.price and the other item fields through their dynamic field controls.

    If you prefer to start from HTML, the section body below declares the same contract. Paste it in the section’s HTML Body editor, replacing the fictional fallback image URL with a valid image for your website:

    <section data-section-type-heading="text"
    data-section-default-heading="Products">
    <h2 data-section-prop-text="heading">Products</h2>
    <ul data-section-repeat="items"
    data-section-item-type-title="text"
    data-section-item-type-price="number"
    data-section-item-type-enabled="boolean"
    data-section-item-type-day="date"
    data-section-item-type-at="datetime"
    data-section-item-type-photo="media">
    <li>
    <h3 data-bind-text="strukt.item.title">Product title</h3>
    <p>Price: <span data-bind-text="strukt.item.price">0</span></p>
    <p>Enabled: <span data-bind-text="strukt.item.enabled">false</span></p>
    <p>Date: <time data-bind-text="strukt.item.day">2026-11-02</time></p>
    <p>Updated: <time data-bind-text="strukt.item.at">2026-11-02T00:00:00Z</time></p>
    <img src="https://images.example.test/fallback.png"
    data-bind-src="strukt.item.photo"
    data-bind-alt="strukt.item.title" alt="Product photo">
    </li>
    <li data-loop-empty>No products available.</li>
    </ul>
    </section>

    The first list item is the repeated template. The data-loop-empty item is reserved for an empty list. These are authoring markers; the public page receives resolved content. Add styles in the normal CSS controls, then select Save section, Publish section and Publish now in its review.

    On a page such as /products, insert the published Product list section through the section library. Select its instance and open Attributes → Section instance.

    For heading, choose Value and enter Featured products. This is a local scalar override; another instance can keep the definition default.

    For Repeater: items, choose Dynamic data, search for Products in List, and select products.$records. Set First N records to 20, then map each of the six item fields to the matching collection field from the table. Every declared item field needs a mapping. A media item field requires a media source field even when its raw value resembles an ordinary URL.

    Review mapping suggestions → Apply mapping suggestions can fill missing exact-name, compatible mappings after your review. Complete any remaining fields manually. If the definition later changes, use the instance’s reviewed property/repeater remap controls before publishing; see input recovery.

    This repeater needs no surrounding collection loop: it receives the collection’s records directly. To bind a scalar property to products.title instead, use an instance inside a Products loop or Products entry template. Select Dynamic data for that scalar, search Dynamic field, and choose products.title. Pick a concrete preview record; a scalar reference does not select the first product automatically.

    For local fallback rows, first choose Rows and add a sample row. Switch back to Dynamic data, select the list and restore all mappings, then enable Use fallback rows when unavailable. Keep 0 and False as explicit values. Use Choose media for website images and Use item fallback to remove one cell’s override. See component properties for default precedence and UTC datetime editing.

    Read Binding preview. This single instance should show the local heading and Dynamic data: 2 rows for items, with the first rows’ values. Confirm Alpha has price 0 and enabled false; neither should be replaced by a fallback. A valid list with zero rows shows the empty state.

    If you later repeat the component in an outer loop, select Preview record or click its exact canvas occurrence before inspecting scalar values. A preview-loading error offers Retry preview data; refreshing upstream data is a separate Sources action.

    Select Save page, then Preview JS → Run preview. Check both products, image URLs, date/time values and your layout. Publish through Publish → Publish now, reviewing any dependency warnings first. Open /products on the public website without an authenticated session and verify the same values.

    On your test API, change Alpha’s title without changing its ID. In Sources, use Refresh now and inspect the report and live snapshot. A successful source refresh can rebuild affected published pages using the new snapshot. Verify the public title; an unsaved layout change is not published by refreshing a source.

    Next, simulate an upstream error on the test API and refresh. The error should remain visible while the existing live snapshot and public page retain their previous successful content. An upstream outage with a retained live snapshot does not by itself activate component fallback rows.

    Test a genuinely empty list separately. A valid empty list uses the empty-state item, but a large record removal can cause the source snapshot to be held for review. Inspect the report and use Release this snapshot only when it is offered and you intend its effect on published pages. Fix schema drift through source mapping rather than treating it as an empty API response.

    Optional: reproduce the real HTTPS acceptance

    Section titled “Optional: reproduce the real HTTPS acceptance”

    The repository includes bun scripts/check-live-api-source.ts [orca-browser-page-id] [screenshot-directory]. It creates and removes a disposable CMS, uses the public synthetic account documented by DummyJSON authentication, and sends real protected HTTPS requests through Strukt’s normal network policy. It does not change your website or the provider’s products.

    The trial maps title, price and thumbnail from /auth/products/category/beauty, with records path products and offset pagination (skip, limit=2). The verified run fetched five products across three pages, rendered them through a component on an anonymous hosted page, refreshed again, rejected an invalid token with HTTP 401 while retaining the last successful HTML, and recovered after credential replacement. The dataset is synthetic; DNS, TLS, authentication and paging use the independently hosted service. This opt-in check needs network access and provider availability. Credentials stay out of its output and screenshots.

    • Products is a read-only external collection with the intended live snapshot.
    • One reusable definition renders two mapped products; its heading is customized on the instance.
    • Number, boolean, date, datetime and media fields keep compatible types, including zero and false.
    • The anonymous page shows the published component and live data; a successful refresh updates the intended content.
    • An upstream failure preserves the last successful public result and leaves a visible failure report.
    Problem Resolution
    Test request says the response is not an array Correct Records path, then test again before mapping.
    Authentication or network request fails Check the public HTTPS endpoint, connection auth and API permissions. Use the connection’s credential settings.
    Products exists but a scalar field is unavailable Put the scalar instance in the correct record scope. Use products.$records for a collection-wide repeater.
    The list falls back or publish blocks Complete every mapping, check matching types and read the property diagnostics.
    Refresh succeeded but public values stay old Compare Latest stored snapshot with Live snapshot and inspect held/release reports. Confirm the page and section are published.
    Retry preview data does not fetch new upstream values Use Refresh now in Sources; preview retry reloads the builder’s data.

    Applies to Strukt f8d2c3ba1848 · 2026-11-03