Create a new project in a group using API


#1

I’m kind of new to the GitLab experience and I’m wanting to automate the local repo creation process with a Linux command line command that will create a project in GitLab using the API. I found the documentation hard to understand. I would like to use curl to do the API project creation. Can anyone give me an example of the HTTPS call (preferably using curl but any help is appreciated) that creates a new specified project in a existing specified group?


Add reposetory to group project
#2

You can try this:

curl --header "PRIVATE-TOKEN: gDGnJwv56z2Xfj2B83Es" -X POST "https://gitlab.com/api/v3/projects?name=foobartest"

where private token can be found in your profile/account. You can add more options as stated in http://doc.gitlab.com/ce/api/projects.html#create-project

for example:

curl --header "PRIVATE-TOKEN: gDGnJwv56z2Xfj2B83Es" -X POST "https://gitlab.com/api/v3/projects?name=foobartest&issues_enabled=false"

PS. I’m in the process of rewriting the docs and the API will have many more examples with curl and other tools.


#3

@axil thanks that helps alot! Lots of examples in the docs would definitely help.


#4

@axil I have successfully created projects using my token. Cool.

But when I tried adding a namespace_id=pyt to my curl command like:

curl --header "PRIVATE-TOKEN: <my token>" -X POST "https://gitlab.com/api/v3/projects?name=foobartest8&namespace_id=pyt"

I get a response of:

{"message":{"namespace":["is not valid"],"limit_reached":[]}}

I could not find anything about a limit in either my profile or group settings. What is limiting this?

Also I could not get the ‘description’ option to work either.


#5

The message seems a little cryptic, but the is not valid answer explains it.

namespace_id is the id of the namespace, where namespace your username or an organization you can create repos to. When you first create a repo you are presented with a json object like so:

{
   "archived" : false,
   "merge_requests_enabled" : true,
   "path" : "foobartest3",
   "default_branch" : null,
   "namespace" : {
      "owner_id" : 3585,
      "updated_at" : "2013-03-21T05:40:35.000Z",
      "membership_lock" : false,
      "path" : "axil",
      "id" : 3467,
      "created_at" : "2013-03-21T05:40:35.000Z",
      "avatar" : null,
      "description" : "",
      "name" : "axil"
   },
   "wiki_enabled" : true,
   "ssh_url_to_repo" : "git@gitlab.com:axil/foobartest3.git",
   "tag_list" : [],
   "http_url_to_repo" : "https://gitlab.com/axil/foobartest3.git",
   "path_with_namespace" : "axil/foobartest3",
   "id" : 416441,
   "created_at" : "2015-08-19T22:10:41.503Z",
   "name_with_namespace" : "Achilleas Pipinellis / foobartest3",
   "description" : null,
   "name" : "foobartest3",
   "web_url" : "https://gitlab.com/axil/foobartest3",
   "public" : false,
   "snippets_enabled" : false,
   "avatar_url" : null,
   "forks_count" : 0,
   "creator_id" : 3585,
   "owner" : {
      "username" : "axil",
      "avatar_url" : "https://gitlab.com/uploads/user/avatar/3585/corto-maltese-rect.jpg",
      "name" : "Achilleas Pipinellis",
      "web_url" : "https://gitlab.com/u/axil",
      "id" : 3585,
      "state" : "active"
   },
   "issues_enabled" : true,
   "visibility_level" : 0,
   "star_count" : 0,
   "last_activity_at" : "2015-08-19T22:10:41.503Z"
}

watch that its namespace_id is 3467. So that’s the number you are looking for. For a list of namespaces you are part of, run:

curl --header "PRIVATE-TOKEN: <token>" "https://gitlab.com/api/v3/namespaces"

As for the description, I guess you used spaces right? Make sure to replace them with %20 or + like:

curl --header "PRIVATE-TOKEN: token" -X POST "https://gitlab.com/api/v3/projects?name=foobartest4&description=This%20is%20a%20description"

See https://en.wikipedia.org/wiki/Percent-encoding for more info.

Tip: If you have perl installed you can pipe the output of the curl commands to | json_pp and the information will be more human readable :smile:


#6

@axil That did it! Thanks! I went looking for an “ID” for namespace in gitlab but I didn’t find it. The gitlab.com/api/v3/namespaces show me all the id for all the groups I’m part of. So I add that and it worked. You also got the space thing right. Didn’t even think of that. Very cool everything thing is working!


#7

You can receive same respond

{“message”:{“namespace”:[“is not valid”],“limit_reached”:[]}}

when you have bad permissions which is really confusing.

User have to have permission at least “Maintainer” on group.


#8

Hi ,
I wanted to create the project on below project location (group/subgroup).

But always the project is getting created on default location of group.

Kindly please do the needful.

Regards,

Santhosh


#9

@santhuhappy24 I also have the same question