DEV Community

Alex Spinov
Alex Spinov

Posted on

Gitlab Has a Free API — Manage Repos, Pipelines, and Issues Without the UI

GitLab API Is More Powerful Than GitHub API

GitLab free tier gives you 2000 API calls per hour, CI/CD pipelines, container registry, and package registry. All accessible via API.

Setup

import requests

TOKEN = "your_personal_access_token"
HEADERS = {"PRIVATE-TOKEN": TOKEN}
BASE = "https://gitlab.com/api/v4"
Enter fullscreen mode Exit fullscreen mode

Create token at gitlab.com/-/user_settings/personal_access_tokens.

List Your Projects

def my_projects(limit=10):
    r = requests.get(f"{BASE}/projects", headers=HEADERS,
                     params={"owned": True, "per_page": limit, "order_by": "last_activity_at"})
    return [{"name": p["name"], "stars": p["star_count"], 
             "url": p["web_url"]} for p in r.json()]

for p in my_projects():
    print(f"{p[name]:30} {p[stars]} stars")
Enter fullscreen mode Exit fullscreen mode

Create a Project

def create_project(name, description, visibility="public"):
    r = requests.post(f"{BASE}/projects", headers=HEADERS, json={
        "name": name, "description": description, "visibility": visibility
    })
    return r.json()["web_url"]

url = create_project("my-api-tool", "A tool that does cool things")
print(f"Created: {url}")
Enter fullscreen mode Exit fullscreen mode

Trigger CI/CD Pipeline

def trigger_pipeline(project_id, ref="main"):
    r = requests.post(f"{BASE}/projects/{project_id}/pipeline",
                      headers=HEADERS, json={"ref": ref})
    return {"id": r.json()["id"], "status": r.json()["status"]}

pipeline = trigger_pipeline(12345)
print(f"Pipeline #{pipeline[id]}: {pipeline[status]}")
Enter fullscreen mode Exit fullscreen mode

List Pipeline Jobs

def get_jobs(project_id, pipeline_id):
    r = requests.get(f"{BASE}/projects/{project_id}/pipelines/{pipeline_id}/jobs",
                     headers=HEADERS)
    return [{"name": j["name"], "status": j["status"], 
             "duration": j.get("duration")} for j in r.json()]
Enter fullscreen mode Exit fullscreen mode

Search Code Across All Projects

def search_code(query):
    r = requests.get(f"{BASE}/search", headers=HEADERS,
                     params={"scope": "blobs", "search": query})
    return [{"project": b["project_id"], "file": b["filename"],
             "data": b["data"][:100]} for b in r.json()[:5]]

results = search_code("API_KEY")
for r in results:
    print(f"Found in {r[file]}: {r[data][:50]}...")
Enter fullscreen mode Exit fullscreen mode

GitLab vs GitHub API

Feature GitHub GitLab
Rate limit 5000/hr 2000/hr
CI/CD API Actions (limited) Full pipeline control
Container registry Packages Built-in
Code search Yes Yes (across all projects)
Free private repos Unlimited Unlimited
Self-hosted Enterprise only Free (Community Edition)

More API tutorials | GitHub


Need custom dev tools, scrapers, or API integrations? I build automation for dev teams. Email spinov001@gmail.com — or explore awesome-web-scraping.


More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs

Top comments (0)