How to create dynamic domain aliases or via API?

Hello,

We’re in an use case where we need to add dynamic domain aliases to our already hosted site on Netlify.

Is there an API or documentation to achieve that? Please respond.

Hi, @iamromec. Why, yes! There is an API:

I have checked the documentation but there is no API of creating domain aliases. Could you specifically share the screenshot or link?

Hi, @iamromec. The open-api link can be found here:

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

Here is a curl example for updating the domain aliases:

curl 'https://api.netlify.com/api/v1/sites/<site API ID>' \
 -X PUT -H 'Authorization: Bearer <redacted token>' \
 --data-raw '{"domain_aliases":["subdomain.example.com"]}'

It does not work. I tried. It does return me the object of that site but, does not update the domain_aliases with existing domain aliases and the newsubdomain.example.com.

curl 'https://api.netlify.com/api/v1/sites/__siteId__' -X PUT -H 'Authorization: Bearer __personal_token__' --data-raw '{"domain_aliases":["newsubdomain.example.com"]}'

Will I have to pass the entire body schema object? If yes, It’s too ridiculous!

Hi, @iamromec.

You don’t need to include the entire schema object. However, you do need to include the complete list of domain aliases.

The API call is not this:

  • add these domain names to the existing domain aliases

The API call is this:

  • change the list of domain aliases to these domain names

If you include only a single domain name, that will become the only domain alias because the API call changes this list to whatever you provide (and you only provided one).

So, you should probably call the API to get the current list (if any). Then add any additional domain aliases to that array and then make the API call to change the aliases with the updated complete array of all domain aliases.

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