71

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?

Lii
  • 9,906
  • 6
  • 53
  • 73
Hubert Ströbitzer
  • 1,185
  • 1
  • 10
  • 18

15 Answers15

55

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
  • 18,032
  • 17
  • 110
  • 225
37

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,222
  • 14
  • 19
  • I cannot see this? What gitlab version? – sunew Feb 15 '17 at 11:56
  • 1
    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. – Bernát Feb 17 '17 at 09:41
33

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

Rakaim
  • 606
  • 7
  • 14
32

You can query for your owned projects:

curl -XGET --header "PRIVATE-TOKEN: XXXX" "https://gitlab.com/api/v3/projects/owned"

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
Fairy
  • 3,116
  • 2
  • 21
  • 36
20

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-ce directly when you want to interact with a project.

Martin Nowak
  • 984
  • 10
  • 8
  • 3
    +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: https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-ce – sactiw Nov 01 '17 at 21:45
20

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,142
  • 11
  • 13
11

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
  • 9,076
  • 5
  • 38
  • 47
qtopierw
  • 1,279
  • 13
  • 22
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,185
  • 1
  • 10
  • 18
  • 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
5

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
  • 114
  • 1
  • 5
4

You can query projects with search attribute e.g:

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

MTZ4
  • 1,985
  • 1
  • 21
  • 32
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
  • 14,255
  • 15
  • 61
  • 83
1

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
  • 277
  • 3
  • 13
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
  • 1,737
  • 1
  • 14
  • 17
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
  • 1,953
  • 14
  • 36
0

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
  • 5,616
  • 3
  • 43
  • 51