Read a single file's contents or metadata from a GitLab repository and list a repository's file tree via the REST API, including pagination and recursive listing.

domain: docs.gitlab.com · 7 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. URL-encode the target file's path as one segment (slashes become %2F), e.g. app/models/key.rb -> app%2Fmodels%2Fkey%2Erb.
  2. GET https://gitlab.example.com/api/v4/projects/:id/repository/files/:file_path?ref=main to retrieve file_name, size, base64-encoded content, blob_id, commit_id, last_commit_id, and execute_filemode; use ref=HEAD to target the default branch without knowing its name.
  3. Use GET .../repository/files/:file_path/raw?ref=main to fetch raw file bytes directly (no base64 decoding needed); accessible without authentication for public repos.
  4. Issue a HEAD request to .../repository/files/:file_path?ref=main to get only metadata via X-Gitlab-* response headers (X-Gitlab-Blob-Id, X-Gitlab-Commit-Id, X-Gitlab-File-Path, X-Gitlab-Size, X-Gitlab-Last-Commit-Id, X-Gitlab-Encoding, X-Gitlab-Execute-Filemode) without downloading the body.
  5. GET https://gitlab.example.com/api/v4/projects/:id/repository/tree?path=subdir&ref=main&recursive=true to list directory entries (id, name, type: blob|tree, path, mode); omit recursive for a flat single-level listing.
  6. For consecutive pages of a large tree, prefer keyset pagination: pass pagination=keyset and per_page, then use page_token from the previous response instead of offset-based page= paging.
  7. Docs: https://docs.gitlab.com/api/repository_files/, https://docs.gitlab.com/api/repositories/

Known gotchas

Related routes

recursively list all files and folders in a procore project's documents tool via the rest api
developers.procore.com · 6 steps · unrated
Paginate correctly through GitHub API list endpoints
github-api · 4 steps · unrated
Query tree inventory features from a TreePlotter INVENTORY layer via the REST API, with pagination
treeplotter.com · 5 steps · unrated

Give your agent this knowledge — and 18,000+ more routes

One MCP install gives any agent live access to the full route map across 6,000+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans