Set build command from API

Hello all,

When I set a build command from the netlify API, it doesn’t reflect on my portal

For example

curl --location --request PATCH 'https://api.netlify.com/api/v1/sites/$SITE_ID' \
--header 'Authorization: Bearer $TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
    "build_settings": {
        "cmd": "yarn build"
    }
}'

Is the body correct?
Thank you!

Saad

1 Like

It all looks correct to me, @saad.bahir.1. However, I do see one issue. These two options need to be double quoted and not single quoted:

'https://api.netlify.com/api/v1/sites/$SITE_ID'

and:

--header 'Authorization: Bearer $TOKEN'

In other words, it should be this:

curl --location --request PATCH "https://api.netlify.com/api/v1/sites/$SITE_ID" \
--header "Authorization: Bearer $TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{
    "build_settings": {
        "cmd": "yarn build"
    }
}'

This is because single quotes will prevent the expansion of the environment variables. If you want the environment variables to be interpreted, then double quotes must be used around those strings.

You can use single quotes elsewhere where no shell expansion is being used.

If this doesn’t fix it, please let us know.

Thank you Luke for your feedback

Unfortunately, the quotes are just a translation of my Postman query to a Curl command, so I didn’t set those quotes

It looks like the PATCH command above actually works

However, when I create a site, that build command doesn’t appear in the response or in the UI

curl --location --request POST 'https://api.netlify.com/api/v1/sites' \
--header 'Authorization: Bearer $TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
  "custom_domain": "customsubdomain",
  "name": "customname",
  "repo": {
    "provider": "gitlab",
    "repo": "username/repo",
    "private": true,
    "branch": "branch"
  },
  "build_settings": {
    "cmd": "yarn build",
    "env": {
      "custom_env": "custom_value"
    }
  }
}'

Hey @saad.bahir.1,

Could you share the site ID in this case?

Hi, @saad.bahir.1. There are a number of topics about this here on the support forums. It is true that not all attributes can be configured on the initial site creation. For those attributes you must create the site with one API call and they update the attributes with a second API call after the first.

If there are other questions, please let us know.