Explore the power of server-side pre-rendering and seamless integration with any tech stack using the Builder HTML API.
To experiment with the HTML API, check out the Builder API Explorer, which works with your own Builder content. The following video shows an example of using the API Explorer:
The following is an example of a request and response:
https://cdn.builder.io/api/v3/html/YOUR_MODEL_NAME?apiKey=YOUR_API_KEY&url=PAGE_URL
# Example response
# {
# "id": "c923kd89",
# "name": "About page",
# "data: {
# "html": "<div data-builder-component="banner-ad"><div class="builder-blocks"><h1>Hello!</h1></div></builder-div>"
# }
# }
Tip: Custom components work in all of Builder's supported frameworks except for the HTML API.
Name | Required | Description |
---|---|---|
| Yes | |
| No | The current URL of the visitor on your site.
|
| No | Set to |
| No | Mongodb-style query of your data.
|
| No | Only include these fields.
|
| No | Omit only these fields.
|
| No | Optionally use this to send targeting attributes.
|
| No | Seconds to cache content (sets the max-age of the cache-control header response header). Set the value higher for better performance and lower for content that changes more frequently.
|
| No | Set to
|
As you work, it is essential to preview the content you build on your actual site. This ensures that any side effects of site-wide styles that affect Builder-created content—for example, heading CSS rules—are accurately displayed.
To integrate previewing:
- Create a single route on your website that serves the Builder preview HTML and JavaScript by creating a new
page/endpoint/route
on your site called, for example,/builder-preview
.
If you name the route something other than /builder-preview
, replace /builder-preview
in any example code and settings to the path you choose.
Your framework determines how to create the new route, but the HTML it returns should be similar to the following example:
<builder-component model="PAGE_MODEL_NAME" api-key="YOUR_API_KEY"></builder-component>
<script async src="https://cdn.builder.io/js/webcomponents"></script>
Tip: Make sure to replace the PAGE_MODEL_NAME
and API_KEY
with the actual values from your Builder space.
For more information, read Using Builder API Keys and Page Models.
Change the PAGE_MODEL_NAME
to the Builder model name of the model you want to preview—oftenm, it is page
. An alternative and useful approach is to not hardcode the model name, but instead to have it be determined by query parameter in the page URL.
In this way, you can use a single endpoint to preview content from different models, such as /builder-preview?model=page
or /builder-preview?model=homepage-section
. Here is a pseudocode example:
import layout from '../yourLayoutComponent';
import { builderPublicKey } from '../constants/builder';
const model = requestQueryParams.get('model');
return (
<layout title="Builder.io Preview page">
<builder-component name="{model}" api-key={builderPublicKey}>
</builder-component>
<script src="https://cdn.builder.io/js/webcomponents" async></script>
</layout>
);
For more information, read Dynamic Preview URLs.
Next, set the model Preview URL in Builder:
- Go to Models.
- Select the model you want to integrate previewing with. Change the preview URL to
https://www.yoursite.com/builder-preview
(make sure to replace that with your actual website). - Click the Save button.
For more context, read Editing and Previewing Your Site.
Tip: If you decided to have the model name determined dynamically, then change the Preview URL to have a model query param and have that value be the name of the model; for example, https://www.yoursite.com/builder-preview?model=page
- Go to Account Settings.
- Go to Advanced Settings > Advanced.
- Toggle off the setting called Reload preview on URL path change. This setting is on by default, so you need to click it to make it turn grey instead of blue, which coincides with the toggle to the left.
You should now be able to create content in the editor and preview it on your site. Repeat this process as needed with other models you'd like to preview on your site.