1

I was trying to automate creating ENV variables in CI/CD by using API commands, but unfortunately getting 401 Unauthorized error.

Earlier I used to do this in same way and was able to do it.
But now it’s throwing an error, could anyone please help me to find out.

Command:

curl --request POST --header “PRIVATE-TOKEN: <your_access_token>” \
  “https://gitlab.com/api/v4/projects/1/variables” --form “key=NEW_VARIABLE” --form “value=new value”

Please look into the error message:

error

1 Answer 1

0

The "Create a variable" API call indeed looks like:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
 "https://gitlab.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"

(make sure to use the right double-quotes " instead of )

Double-check the ID of the project (in your case "1") and make sure the user authenticated with your token has the right permissions:

They must be "maintainer" or "owner", in order to have the right to "Manage project-level CI/CD variables".


Example, using a Personal Access Token with api scope, starting with glpat- (glpat-xxxxxxx), I can first list my projects (using jq):

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/owned?=true"|jq ".[] | \"\(.id) \(.path_with_namespace)\""

That allows me to find the project id

I can then list variables for an existing project:

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"

Result:

[]

I have none on that project.
I will set one with:

curl -XPOST --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables" --form "key=NEW_VARIABLE" --form "value=new value"

Result:

{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}

Let's double-check with:

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"

Result:

[{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}]

It does work.


The OP Anirban Das confirms in the comments an issue with how Postman was used:

Actually in the body of Postman, select 'form', there I had mentioned directly key name in the Key section and value in the Value section.
But that was not correct.

In the Key section, we need to mention "key" and key name should be in Value section.
Similarly "value" in key section and it's value in Value section.

Once this worked, you will see "</>" icon in right navigation pane, which will provide you corresponding curl command

Sign up to request clarification or add additional context in comments.

14 Comments

Yes, I have given correct project ID and used double quote as well (""). PFA screenshot. Also, in this project I am owner.
I tried to call API from Postman, there i am getting "error: key is missing, value is missing" error.
@AnirbanDas That soulds like your potman query did not send the form values.
yes. can you pls try from end and show me how it's behaving @VonC
@AnirbanDas I just tested the command. It does work. I have edited the answer to include an example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.