Getting started with the API

python-gitlab supports both GitLab v3 and v4 APIs.

v3 being deprecated by GitLab, its support in python-gitlab will be minimal. The development team will focus on v4.

v4 is the default API used by python-gitlab since version 1.3.0.

gitlab.Gitlab class

To connect to a GitLab server, create a gitlab.Gitlab object:

import gitlab

# private token or personal token authentication
gl = gitlab.Gitlab('', private_token='JVNSESs8EwWRx5yDxM5q')

# oauth token authentication
gl = gitlab.Gitlab('', oauth_token='my_long_token_here')

# username/password authentication (for GitLab << 10.2)
gl = gitlab.Gitlab('', email='jdoe', password='s3cr3t')

# anonymous gitlab instance, read-only for public resources
gl = gitlab.Gitlab('')

# make an API request to create the gl.user object. This is mandatory if you
# use the username/password authentication.

You can also use configuration files to create gitlab.Gitlab objects:

gl = gitlab.Gitlab.from_config('somewhere', ['/tmp/gl.cfg'])

See the Configuration section for more information about configuration files.

Note on password authentication

The /session API endpoint used for username/password authentication has been removed from GitLab in version 10.2, and is not available on anymore. Personal token authentication is the preferred authentication method.

If you need username/password authentication, you can use cookie-based authentication. You can use the web UI form to authenticate, retrieve cookies, and then use a custom requests.Session object to connect to the GitLab API. The following code snippet demonstrates how to automate this:

See issue 380 for a detailed discussion.

API version

python-gitlab uses the v4 GitLab API by default. Use the api_version parameter to switch to v3:

import gitlab

gl = gitlab.Gitlab('', 'JVNSESs8EwWRx5yDxM5q', api_version=3)


The python-gitlab API is not the same for v3 and v4. Make sure to read Switching to GitLab API v4 if you are upgrading from v3.


The gitlab.Gitlab class provides managers to access the GitLab resources. Each manager provides a set of methods to act on the resources. The available methods depend on the resource type.


# list all the projects
projects = gl.projects.list()
for project in projects:

# get the group with id == 2
group = gl.groups.get(2)
for group in groups:

# create a new user
user_data = {'email': '', 'username': 'jen', 'name': 'Jen'}
user = gl.users.create(user_data)

You can list the mandatory and optional attributes for object creation with the manager’s get_create_attrs() method. It returns 2 tuples, the first one is the list of mandatory attributes, the second one the list of optional attribute:

# v4 only
(('name',), ('path', 'namespace_id', ...))

The attributes of objects are defined upon object creation, and depend on the GitLab API itself. To list the available information associated with an object use the python introspection tools for v3, or the attributes attribute for v4:

project = gl.projects.get(1)

# v3
# or

# v4

Some objects also provide managers to access related GitLab resources:

# list the issues for a project
project = gl.projects.get(1)
issues = project.issues.list()

Gitlab Objects

You can update or delete a remote object when it exists locally:

# update the attributes of a resource
project = gl.projects.get(1)
project.wall_enabled = False
# don't forget to apply your changes on the server:

# delete the resource

Some classes provide additional methods, allowing more actions on the GitLab resources. For example:

# star a git repository
project = gl.projects.get(1)

Base types

The gitlab package provides some base types.

  • gitlab.Gitlab is the primary class, handling the HTTP requests. It holds the GitLab URL and authentication information.

For v4 the following types are defined:

  • gitlab.base.RESTObject is the base class for all the GitLab v4 objects. These objects provide an abstraction for GitLab resources (projects, groups, and so on).
  • gitlab.base.RESTManager is the base class for v4 objects managers, providing the API to manipulate the resources and their attributes.

For v3 the following types are defined:

  • gitlab.base.GitlabObject is the base class for all the GitLab v3 objects. These objects provide an abstraction for GitLab resources (projects, groups, and so on).
  • gitlab.base.BaseManager is the base class for v3 objects managers, providing the API to manipulate the resources and their attributes.

Lazy objects (v4 only)

To avoid useless calls to the server API, you can create lazy objects. These objects are created locally using a known ID, and give access to other managers and methods.

The following example will only make one API call to the GitLab server to star a project:

# star a git repository
project = gl.projects.get(1, lazy=True)  # no API call  # API call


If you have the administrator status, you can use sudo to act as another user. For example:

p = gl.projects.create({'name': 'awesome_project'}, sudo='user1')

Advanced HTTP configuration

python-gitlab relies on requests Session objects to perform all the HTTP requests to the Gitlab servers.

You can provide your own Session object with custom configuration when you create a Gitlab object.

Context manager

You can use Gitlab objects as context managers. This makes sure that the requests.Session object associated with a Gitlab instance is always properly closed when you exit a with block:

with gitlab.Gitlab(host, token) as gl:


The context manager will also close the custom Session object you might have used to build a Gitlab instance.

Proxy configuration

The following sample illustrates how to define a proxy configuration when using python-gitlab:

import gitlab
import requests

session = requests.Session()
session.proxies = {
    'https': os.environ.get('https_proxy'),
    'http': os.environ.get('http_proxy'),
gl = gitlab.gitlab(url, token, api_version=4, session=session)


Client side certificate

The following sample illustrates how to use a client-side certificate:

import gitlab
import requests

session = requests.Session()
s.cert = ('/path/to/client.cert', '/path/to/client.key')
gl = gitlab.gitlab(url, token, api_version=4, session=session)


Rate limits

python-gitlab will obey the rate limit of the GitLab server by default. On receiving a 429 response (Too Many Requests), python-gitlab will sleep for the amount of time in the Retry-After header, that GitLab sends back.

If you don’t want to wait, you can disable the rate-limiting feature, by supplying the obey_rate_limit argument.

import gitlab
import requests

gl = gitlab.gitlab(url, token, api_version=4)
gl.projects.list(all=True, obey_rate_limit=False)


You will get an Exception, if you then go over the rate limit of your GitLab instance.