GitLab is an open-source collaborative git platform with tons of features. Gitlab offers a broad range of features for its users to make git more collaborative and accessible. It offers free tier for users with personal and hobby projects. Users can create private repositories if they don’t want the code to be public. Similar to GitHub’s gists, GitLab has its snippets. Snippets are short standalone programs that usually server a specific purpose. You can have private snippets as well. Here’s how you access your private snippets remotely without logging into your GitLab account.
GitLab has its documented API access for files in a repository, but it’s missing them for snippets. The document lists accessing snippets inside a project here, but it targets snippets that are already inside a project. It says nothing about standalone snippets not inside a project.
You will need:
- A GitLab private access token(requires login) to access the file remotely or through your terminal.
- The ID of your snippet.
- General understanding of curl
Now you will have to format a curl syntax to make it grab your snippets file. The private access token will verify our request so the file inaccessible to anyone but us.
curl --header “PRIVATE-TOKEN: <private-access-token>” “https://gitlab.com/api/v4/snippets/<snippet-id>/raw” -o main.py
curl --header “PRIVATE-TOKEN: 2LAX6jyj4f8Zb5fd5Ft4yA” “https://gitlab.com/api/v4/snippets/9999999/raw” -o main.py
The “main.py” is file name you want the output to be pasted to. It can be any name you prefer the file name and extension to be saved. Obviously, it it is a source code file, you’ll have to name it properly, but the idea is that It doesn’t have to reflect the exact filename on your snippet.
If all goes well, you will see a file download progress and the file should appear on your folder. Notice the “Total” column, it should have a file size information. It’s a sign that the command worked.