How to enable prerendering via api

How does one enable prerendering via the API?

Hi, @phishy.

tl;dr - “copy as cURL” is your best friend for these types of issues.

In almost all cases about how to do something with our API, my advice is this:

  • Make the change in our web UI with the browser developer tools open and examine the API calls there directly.

You can also consult our open API documentation but seeing a real working API call takes all of the guess work out of it (at least for me).

This is what I get with the “copy as cURL” option:

curl 'https://api.netlify.com/api/v1/sites/{id}' \
-X PUT -H 'Content-Type: application/json' \
-H 'Authorization: Bearer THIS_IS_NOT_A_REAL_TOKEN' --data-raw '{"prerender":"netlify"}'

Technically this will work without the content-type header but I’ve included it because it isn’t sensitive and it is “more proper”. Obviously, you will need to replace the site id and auth token to use the example above. I hope it clarifies how the API call is made though.

To disable use {"prerender":null} instead.

If there are other questions or concerns, please let us know.

Hi,

I’m creating site using Netlify API with curl with following options :
{“name”:“Dynamic 01”,“force_ssl”:false,“prerender”:“netlify”,“processing_settings”:{“skip”:false,“css”:{“bundle”:true,“minify”:true},“js”:{“bundle”:true,“minify”:true},“html”:{“pretty_urls”:true,“canonical_urls”:true},“images”:{“optimize”:true}}}

But Netlify prerendering is not enabled. Is something wrong?

Thanks.

Could you share the site name and the full command you’re trying?

Site name is “Dynamic 01” and curl command is :

curl --header “Authorization: Bearer MY_TOKEN” --header “Content-Type: application/json” -X POST https://api.netlify.com/api/v1/sites -d ‘{“name”:“Dynamic 01”,“force_ssl”:false,“prerender”:“netlify”,“processing_settings”:{“skip”:false,“css”:{“bundle”:true,“minify”:true},“js”:{“bundle”:true,“minify”:true},“html”:{“pretty_urls”:true,“canonical_urls”:true},“images”:{“optimize”:true}}}’

Hi, @m3ways. It is the site name that is the issue. The site name is the subdomain for the site under netlify.app and subdomains cannot contain spaces and will always be all lower case in our database. In other words, the site name can be dynamic-01 but never Dynamic 01.

You can also use the Netlify CLI tool to make API calls. The CLI tools was designed to be easily used with other scripts and CI/CD systems. For example, you can have node script or bash scripts run Netlify CLI commands. Using this method scripts and other programs can perform manual site deploys or make calls to the APIs at Netlify.

That process would start with getting a API key/token. The token can be stored as an environment variable on your local system or on a CI/CD systems (whatever you are using).

Note, the CLI API methods expect the “site API id” or “site id”, which can be found in the web UI under “Site Name” > Settings > General > Site information.

Let’s pretend the site id is 6a660c98-0673-4d18-8187-304c94706f73. Using that site id as an example, you can enable prerendering via the API with the Netlify CLI like so:

netlify api updateSite --data '{ "site_id": "6a660c98-0673-4d18-8187-304c94706f73", "body": { "prerender": "netlify" } }'

And disable it like so:

netlify api updateSite --data '{ "site_id": "6a660c98-0673-4d18-8187-304c94706f73", "body": { "prerender": null } }'

If you are looking for a pure curl solution, it would look something like this (and please note the site_id is being sent as part of the URL itself and not the JSON data (meaning not in the --data-raw field):

# enable prerendering via API with curl

curl 'https://api.netlify.com/api/v1/sites/6a660c98-0673-4d18-8187-304c94706f73' \
  -X PUT -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <BEARER TOKEN HERE>' \
  --data-raw '{"prerender":"netlify"}'

# disable prerendering via API with curl

curl 'https://api.netlify.com/api/v1/sites/6a660c98-0673-4d18-8187-304c94706f73' \
  -X PUT -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <BEARER TOKEN HERE>' \
  --data-raw '{"prerender":null}'

If there are other questions or if neither of those solutions above work for you, please let us know.

Hi,

Don’t kwow how, but I was able to create site “Dynamic 01”, but I can’t no more…
Anyway, I’ve created “dynamic-01” and used updateSite to enable prerendering and all is fine now !

Thanks !

1 Like