Assign domain to Netlify site via API

Hi community,
I am using Workers with netlify Apis to create new site and its working great
Using this endpoint

https://api.netlify.com/api/v1/deploys/{deployId}/files{filePath}
https://api.netlify.com/api/v1/sites/{my-siteId}/deploys

upon site creation i get the created site name, id URL etc.

Now i want to assign custom domain to that created site,
so i am trying with this endpoint

https://api.netlify.com/api/v1/sites/{my-siteId}/domains

but sadly, after weeks of work no results,
can you guide me to correct API and its doc (if any)
i also tried to inspect on UI to look for APIs but unable to get the proper one.

Thanks in advance

Hi, @Siddharth. Our recommendation for understanding our API is this:

  • opened devtools when using app.netlify.com
  • click on the network tab
  • make the change needed via the web UI
  • inspect the API call in devtools to find the correct syntax

The API documentation for making domain changes is here:

https://open-api.netlify.com/#tag/site/operation/updateSite

The custom_domain attribute is part of the site object and it is updated there.

Note, the URLs that the UI use are slightly different and look like this:

https://app.netlify.com/access-control/bb-api/api/v1/sites/<site id goes here>

The JSON payload looks like this:

{"custom_domain":"subdomain.example.com"}

Please let us know if that does not work as promised or if there are other questions.

I also want to mention one more important detail to know about the site creation APIs at Netlify. It is a detail which is not well documented (but I have discussed it repeatedly in these support forum posts) which is this:

  • most settings cannot be configured when creating the site during the initial API call

This means that two API calls are required to set all settings for a site. The two API calls are:

  • an initial API call to create the site in the database
  • a second API call to update all the other attributes of the site after it exists

So, if you try to set a custom domain when creating a site, it will not work. However, if you create the site with one API call and then update the custom domain with a second API call, that will succeed.

1 Like