155

I use GitLab on their servers. I would like to download my latest built artifacts (build via GitLab CI) via the API like this:

curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.com/api/v3/projects/1/builds/8/artifacts" 

Where do I find this project ID? Or is this way of using the API not intended for hosted GitLab projects?

2
  • I created an issue: gitlab.com/gitlab-org/gitlab-ce/issues/28342 Commented Feb 17, 2017 at 12:45
  • 1
    A GitLab project's main page should come with the "More actions" menu (button with three vertical dots; usually top right) which shows the ID when clicked. Commented Sep 25, 2024 at 14:05

24 Answers 24

101

I just found out an even easier way to get the project id: just see the HTML content of the gitlab page hosting your project. There is an input with a field called project_id, e.g:

<input type="hidden" name="project_id" id="project_id" value="335" /> 
Sign up to request clarification or add additional context in comments.

3 Comments

Great answer. Despite not guaranteed to work since does not use any API, it is quite possible this approach will remain working indefinitely
Thanks, I didn't see it right away but was able to print it to the console with a little js: document.getElementById('project_id').value
This no longer works, but this does: document.body.attributes['data-project-id']
99

The latest version of GitLab 11.4 at the time of this writing now puts the Project ID at the top of the frontpage of your repository.

Screenshot:

GitLab Project Front Page

4 Comments

Worth noting, you can click to copy the ID to your clipboard
That's not very useful when the project path needs to be translated to its ID dynamically for automation purposes.
For a private repo where I don't have access, I can't see this Project ID
No longer true as of Gitlab v17.11 at least, see stackoverflow.com/a/78151772/1333493
51

On the Edit Project page there is a Project ID field in the top right corner.

(You can also see the ID on the CI/CD pipelines page, in the exameple code of the Triggers section.)

In older versions, you can see it on the Triggers page, in the URLs of the example code.

3 Comments

I cannot see this? What gitlab version?
In the version which is running on Gitlab.com... i don't know exactly, but not long ago, maybe since January 2017. In older versions you can find it in the example code on the Triggers page.
Goto Settings > General
48

You can query for your owned projects:

curl -XGET --header "PRIVATE-TOKEN: XXXX" "https://gitlab.com/api/v4/projects?owned=true" 

You will receive JSON with each owned project:

[ { "id":48, "description":"", "default_branch":"master", "tag_list":[ ... 

You are also able to get the project ID from the triggers configuration in your project which already has some sample code with your ID.

From the Triggers page:

curl -X POST \ -F token=TOKEN \ -F ref=REF_NAME \ https://<GitLab Installation>/api/v3/projects/<ProjectID>/trigger/builds 

10 Comments

great thx - helps a lot. Small note: I think -header should be --header.
No problem. Don't forget to accept the answer if it has solved your problem.
This answer is not valid anymore. See answer by @Bernát. You can see the project ID on the project settings page.
@Fairy Go to Settings -> CI/CD Pipelines. On the Triggers section, the URL examples contain the project ID. e.g. "gitlab.com/api/v3/projects/xxxxxx/trigger/builds"
v3_to_v4.md, "/api/v4/projects?owned=true"
|
42

As mentioned here, all the project scoped APIs expect either an ID or the project path (URL encoded). So just use https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-foss directly when you want to interact with a project.

2 Comments

+1 important point is that NAMESPACE/PROJECT_NAME mentioned after /api/v4/projects/ must be URL encoded. That means / needs to be replaced with %2F For example: gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-ce
This example is 404 for me.
29

Enter the project.

On the Left Hand menu click Settings -> General -> Expand General Settings

It has a label Project ID and is next to the project name.

This is on version GitLab 10.2

Comments

20

Provide the solution that actually solve the problem the api of getting the project id for specific gitlab project

curl -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" http://<YOUR-GITLAB-SERVER>/api/v3/projects/<YOUR-NAMESPACE>%2F<YOUR-PROJECT-NAME> | python -mjson.tool 

Or maybe you just want the project id:

curl -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" http://<YOUR-GITLAB-SERVER>/api/v3/projects/<YOUR-NAMESPACE>%2F<YOUR-PROJECT-NAME> | python -c 'import sys, json; print(json.load(sys.stdin)["id"])' 

Note that the repo url(namespace/repo name) is encoded.

4 Comments

In case you are using gitlab.com the namespace would be your username.
wouldn't the namespace be the owner of the project you are trying to get the project id for, rather than always your own username?
And the one based on jq: curl -s -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" http://<YOUR-GITLAB-SERVER>/api/v3/projects/<YOUR-NAMESPACE>%2F<YOUR-PROJECT-NAME> | jq '.id
Thanks! works also with API v4
19

In new GitLab version you must click settings in right side Project page and copy Project IDenter image description here

2 Comments

To be clear, you need to click the 3 vertical dots to get that to pop up. This works even if you don't have permission to see the "Settings" page of the repo.
Wow this isn't intuitive. This should be the currently accepted answer.
9

You can view it under the repository name

enter image description here

Comments

9

If you know your project name, you can get the project id by using the following API:

curl --header "Private-Token: <your_token>" -X GET https://gitlab.com/api/v4/projects?search=<exact_project_name> 

This will return a JSON that includes the id:

[ { "id":<project id>, ... } ] 

Comments

6

Just for the record, if someone else has the need to download artifacts from gitlab.com created via gitlab-ci

  1. Create a private token within your browser
  2. Get the project id via curl -XGET --header "PRIVATE-TOKEN: YOUR_AD_HERE?" "https://gitlab.com/api/v3/projects/owned"
  3. Download the last artifact from your master branch created via a gitlab-ci step called release curl -XGET --header "PRIVATE-TOKEN: YOUR_AD_HERE?" -o myapp.jar "https://gitlab.com/api/v3/projects/4711/builds/artifacts/master/download?job=release"

I am very impressed about the beauty of gitlab.

1 Comment

v3 is no longer available. The URL is now: https://gitlab.com/api/v4/projects/PROJECT_ID/jobs/artifacts/master/raw/PATH_TO_FILE?job=JOB
4

You can query projects with search attribute e.g:

http://gitlab.com/api/v3/projects?private_token=xxx&search=myprojectname

Comments

4

As of GitLab API v4, the following API returns all projects that you own:

curl --header 'PRIVATE-TOKEN: <your_token>' 'https://gitlab.com/api/v4/projects?owned=true' 

The response contains project id. GitLab access tokens can be created from this page- https://gitlab.com/profile/personal_access_tokens

Comments

3

That was unrelated to the question, but because Google makes this question appear first when you want to just find the project-id in the new GitLab UI, here you go.

The simplest option to find the project-id:

Find the project-id.

If this option is not available, you can search it on the source page:

  1. Right-click the main GitLab page of your project
  2. Select "View Page Source" (Ctrl + U in Chrome)
  3. Search for:
Project ID: 

There you go.

Comments

2

No answer suits generic needs, the most similar is intended only for the gitlab site, not specific sites. This can be used to find the ID of the project streamer in the GitLab server my-server.com, for example:

$ curl --silent --header 'Authorization: Bearer MY-TOKEN-XXXX' \ 'https://my-server.com/api/v4/projects?per_page=100&simple=true'| \ jq -rc '.[]|select(.name|ascii_downcase|startswith("streamer"))'| \ jq .id 168 

Remark that

  • this gives only the first 100 projects, if you have more, you should request the pages that follow (&page=2, 3, ...) or run a different API (e.g. groups/:id/projects).
  • jq is quite flexible. Here we're just filtering a project, you can do multiple things with it.

1 Comment

definitely better answer!
1

enter image description here

As of this writing year 2024, you can find the project ID:

  1. Click 3 dots at the upper right side
  2. That's it!

Comments

1

There appears to be no way to retrieve only the Project ID using the GitLab API. Instead, retrieve all the owner's projects and loop through them until you find the matching project, then return the ID. I wrote a script to get the project ID:

#!/bin/bash projectName="$1" namespace="$2" default=$(sudo cat .namespace) namespace="${namespace:-$default}" json=$(curl --header "PRIVATE-TOKEN: $(sudo cat .token)" -X GET 'https://gitlab.com/api/v4/projects?owned=true' 2>/dev/null) id=0 idMatch=0 pathWithNamespaceMatch=0 rowToMatch="\"$(echo "$namespace/$projectName" | tr '[:upper:]' '[:lower:]')\"," for row in $(echo "${json}" | jq -r '.'); do [[ $idMatch -eq 1 ]] && { idMatch=0; id=${row::-1}; } [[ $pathWithNamespaceMatch -eq 1 ]] && { pathWithNamespaceMatch=0; [[ "$row" == "$rowToMatch" ]] && { echo "$id"; return 0; } } [[ ${row} == "\"path_with_namespace\":" ]] && pathWithNamespaceMatch=1 [[ ${row} == "\"id\":" ]] && idMatch=1 done echo 'Error! Could not retrieve projectID.' return 1 

It expects the default namespace to be stored in a file .namespace and the private token to be stored in a file .token. For increased security, its best to run chmod 000 .token; chmod 000 .namespace; chown root .namespace; chown root .token

Comments

0

Not Specific to question, but somehow reached here, might help others I used chrome to get a project ID

  1. Go to the desired project example gitlab.com/username/project1
  2. Inspect network tab
  3. see the first garphql request in network tab enter image description here

Comments

0

My favorite method is to pull from the CI/CD pipeline so on build it dynamically assigns the project id.

Simply assign a variable in your code to = CI_PROJECT_ID

Comments

0

Very simple, enter in address bar in google chrome: https://gitlab.bluhbluhbluh.com/api/v4/groups/a_name_of_your_group

Then, you can search id of all projects in the selected group.

Comments

0

Posting this answer if someone's using the official glab cli, you can get the project id of the current project using:

glab repo view -F json | jq '.id' 

not very obvious in the cli

Comments

0

To get id from all projects, use:

curl --header 'PRIVATE-TOKEN: XXXXXXXXXXXXXXXXXXXXXXX' 'https://gitlab.com/api/v4/projects?owned=true' > curloutput grep -oPz 'name\":\".*?\"|{\"id\":[0-9]+' curloutput | sed 's/{\"/\n/g' | sed 's/name//g' |sed 's/id\"://g' |sed 's/\"//g' | sort -u -n 

Comments

0

You can search for the project path

curl -s 'https://gitlab.com/api/v4/projects?search=my/path/to/my/project&search_namespaces=true' --header "PRIVATE-TOKEN: $GITLAB_TOKEN" |python -mjson.tool |grep \"id\" 

https://docs.gitlab.com/ee/api/projects.html

Which will only match your project and will not find other unnecessary projects

Comments

0

If your project name is unique, it is handy to follow the answer by shunya, search by name, refer API doc.

If you have stronger access token and the GitLab contains a few same name projects within different groups, then search within group is more convenient. API doc here. e.g.

curl --header "PRIVATE-TOKEN: <token>" -X GET https://gitlab.com/api/v4/groups/<group_id>/search?scope=projects&search=<project_name> 

The group ID can be found from the Settings page under the group domain.

And to fetch the project id from the output, you can do:

curl --header "PRIVATE-TOKEN: <token>" -X GET https://gitlab.com/api/v4/groups/<group_id>/search?scope=projects&search=<project_name> | jq '[0].id' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.