Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find out base GitLab API base url

Tags:

gitlab

api

I've created project and repo on my gitlab.com account, generated private key, now I'm trying to do api call to get list of commits.

Now I want to get list of projects via api, from documentation https://docs.gitlab.com/ce/api/projects.html#list-projects

GET /projects 

So I'm doing

curl --header "PRIVATE-TOKEN: XXXXXXX -c" "https://gitlab.com/projects" 

And getting 404. I've tried several combinations and can't find correct base url.

Same for repository commits, documentations https://docs.gitlab.com/ce/api/commits.html says

https://gitlab.example.com/api/v3/projects/5/repository/commits 

fine, I'm trying (with myusername/projectname as project id) https://gitlab.com/api/v3/projects/myusername/projectname/repository/commits

And got 404 as well

like image 572
Andriy Kopachevskyy Avatar asked Sep 28 '16 15:09

Andriy Kopachevskyy


People also ask

Where is my GitLab API URL?

If you're a GitLab.com user, the base URL is https://gitlab.com . If you're hosting your own GitLab instance, the base URL depends on your domain configuration so you may need to consult your GitLab admin in case you're unsure (although it's ok to use a trial and error too.

Where is GitLab API key?

To locate your personal GitLab API key, first navigate to the GitLab User Settings from the main menu. Then click on Access Tokens in the left side menu. Give a descriptive name for your token, select the “api” scope and finally click on the Create personal access token button.

What is GitLab API?

The GitLab API allows you to perform many of the actions you typically do when using the user interface. Using the API allows us to automate the process and eliminate any user interaction. The GitLab API comes in two flavors: the REST API and the GraphQL API.


2 Answers

The correct base url for the hosted GitLab is https://gitlab.com/api/v4/ so your request to
GET /projects would be

curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/projects" 

That would return all projects that are visible to you, including other user's public projects.

If you wish to view just your projects, then you should use the GET /users/:user_id/projects endpoint, where :user_id is your user ID that can be found on your GitLab profile page or in the response to your request to GET /user if you're authenticated.

# Get :user_id from this request curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/user"  # See your projects by replacing :user_id with id value from previous request curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/users/:user_id/projects" 

Also, the project ID is not the same as the project name. You can retrieve the project ID from the response of your request to GET /users/:user_id/projects, or from the project's settings page.

like image 50
BrokenBinary Avatar answered Oct 04 '22 03:10

BrokenBinary


For me the following request worked:

curl --header "PRIVATE-TOKEN: YOUR_TOKEN" "https://gitlab.com/api/v4/users/YOUR_USER_ID/projects"  

Don't know why the request :curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/projects/" returned a list with some other public projects.

Another useful request for user info: curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/user/"

like image 42
dejanualex Avatar answered Oct 04 '22 05:10

dejanualex