Open API JS client, patch

Hi,

I’m having trouble figuring out how to patch via the API, I’m trying to update aliases in Express JS.

I’ve created a function here…

const updateSiteAliases = async function() {
    return await client.updateSite({
        site_id: 'xxxxxxx',
    }).then(data => { return data })
}
const UpdateSiteAliasesResult = updateSiteAliases()

and the route here…

router.patch('/update-alias/', (req, res) => {
    UpdateSiteAliasesResult.then(function(result) {
       res.send(req.body)
    })
});

I’ve authenticated, and added site_id in the function and it’s returning the site data. I’m sending a JSON payload of…

{
  "domain_aliases": [
    "updatedviaapi.domain.com"
  ]
}

but i’m not seeing any changes to the domain aliases. Am I missing something? :S

Thanks a lot!

Hey @lemongrab!
Do you have a custom domain set? If not, you should get this error:

{
    "code": 422,
    "message": "You cannot update domain aliases while primary custom domain is not set"
}

But assuming you do, I think you’ll want a request that’s structured more like client.createSite() here:

So:

const site = await client.updateSite({
  site_id: 'xxxxxx',
  body: {
    domain_aliases: ["updatedviaapi.domain.com"]
  }
})

Let us know if that works any better for you!

Thanks @jen!

I think I must be missing something fundamental.
Adding body: { domain_aliases: ["updatedviaapi.domain.com"] } results in ‘Unprocessable Entity’

I don’t get any errors back, just the data for the site id, account name, slug etc, but the domain aliases don’t update. I’m getting { domain_aliases: [ 'updatedviaapi.ionicfireapp.com' ] } back as req.body after posting.

Is there something I’m totally missing with this? :S

client.updateSite({
            site_id: 'xxxxx'
        }).then(data => {
            res.send(data); //here's the unchanged data
        })
        .catch(err => {
            res.status(500).send({
                message: err.message || "error"
            });
        });

Thanks!

You will definitely need to send the body along with the site_id- otherwise, we won’t know that you want to update a domain alias on that site or what to update it to! You might have gotten “Unprocessable Entity” from the the res.send, not the initial API call.

I was able to deploy this Netlify Function on one of my sites, hit the endpoint, and have it update my domain alias in the UI (and return the updated domain alias):

const NetlifyAPI = require('netlify')
const client = new NetlifyAPI(process.env.NETLIFY_PAT)

// exports.handler stuff is only for Netlify Functions; you won't need for just using the js-client
exports.handler = async function (event, context) {
  // let's use the updateSite(), as you have been doing
  const site = await client.updateSite({
      // put in your site_id; make sure it's correct!
      site_id: "628de7ef-04fb-4530-a7da-b795c0e5a457",
      // with this, we're saying "for the above site_id, update the domain alias to this thing in the body"
      body: { 
          domain_aliases: ["flippers.netlify.horse"] 
        }
    })
    // when that's done, log what is returned; should include your new domain alias 
    .then(data => console.log(data))
    // if something goes wrong, surface the error
    .catch(e => console.log('Error updating domain alias', e))
}

Can you start with that and see if you get it working before incorporating middleware (res.send, etc.)? I think we’re getting closer :muscle:

1 Like

amazing, thanks so much @jen! You really helped me out, that’s all working now!

I was getting ‘Error updating domain alias JSONHTTPError: Unprocessable Entity’ at one point, I think I might have been adding an incorrect domain alias.

Thanks again!
:tada: :muscle:

1 Like