123

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Hubert Ströbitzer
  • 1,745
  • 2
  • 15
  • 19

19 Answers19

88

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" />
codependent
  • 23,193
  • 31
  • 166
  • 308
  • Great answer. Despite not guaranteed to work since does not use any API, it is quite possible this approach will remain working indefinitely – Leonardo Maffei Nov 29 '21 at 20:04
  • 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` – justFatLard Oct 04 '22 at 17:03
83

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

Kurt
  • 1,169
  • 1
  • 9
  • 16
50

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.

Bernát
  • 1,362
  • 14
  • 19
44

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Fairy
  • 3,592
  • 2
  • 27
  • 36
34

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.

Alex Gittemeier
  • 5,224
  • 30
  • 55
Martin Nowak
  • 1,282
  • 13
  • 8
25

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

Martin Naughton
  • 1,476
  • 17
  • 14
13

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.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
qtopierw
  • 1,447
  • 14
  • 23
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>, ...
  }
]
shunya
  • 154
  • 1
  • 5
7

You can view it under the repository name

enter image description here

quoc9x
  • 1,423
  • 2
  • 9
  • 26
5

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.

Hubert Ströbitzer
  • 1,745
  • 2
  • 15
  • 19
  • 3
    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` – AndrewHarvey Feb 27 '19 at 02:40
4

You can query projects with search attribute e.g:

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

MTZ4
  • 2,274
  • 2
  • 25
  • 41
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

jerrymouse
  • 16,964
  • 16
  • 76
  • 97
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.
RodolfoAP
  • 743
  • 1
  • 7
  • 18
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

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
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'
Chuan
  • 3,103
  • 1
  • 19
  • 23
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
Ferroao
  • 3,042
  • 28
  • 53
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
Divek John
  • 623
  • 8
  • 16
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

fargusto
  • 163
  • 3
  • 4
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

sixfears7
  • 66
  • 4