Synthetic Monitor Secure Credential Not Found without a Manual Save

I’m uploading my synthetic monitor script

curl -s -X PUT -H "Api-Key:$API_KEY" -H 'Content-Type:application/json' "$LOCATION/script" -d

in the official doc

https://docs.newrelic.com/docs/apis/synthetics-rest-api/monitor-examples/manage-synthetics-monitors-rest-api/

I need to reference a secure credential inside my script with $secure.SOME_VARIABLE. However, when I first uploaded the script, the variable couldn’t be found. I would have to go to synthetic Monitor → settings → script and then manually save it without doing any change . Then it would work. Not sure has anyone met this problem before and any workarounds or comments would be appreciated.

I think I figured out the problem. So I must create a secure credential before creating my monitor. The other way around will not correctly reference the variable without a manual reload (save)