Help us learn about your current experience with the documentation. Take the survey.
Snippets API
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
Snippets API 作用于 snippets。相关 API 包括 项目代码片段 和 在存储之间移动代码片段。
代码片段可见性级别
GitLab 中的代码片段可以是私有、内部或公开的。
您可以通过代码片段中的 visibility 字段设置它。
代码片段可见性级别的有效值为:
| 可见性 | 描述 |
|---|---|
private |
代码片段仅对创建者可见。 |
internal |
代码片段对任何已认证用户可见(不包括 外部用户)。 |
public |
代码片段无需任何认证即可访问。 |
列出当前用户的所有代码片段
获取当前用户的所有代码片段列表。
GET /snippets参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
per_page |
integer | 否 | 每页返回的代码片段数量。 |
page |
integer | 否 | 要检索的页码。 |
created_after |
datetime | 否 | 返回指定时间之后创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z) |
created_before |
datetime | 否 | 返回指定时间之前创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z) |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets"示例响应:
[
{
"id": 42,
"title": "Voluptatem iure ut qui aut et consequatur quaerat.",
"file_name": "mclaughlin.rb",
"description": null,
"visibility": "internal",
"imported": false,
"imported_from": "none",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://example.com/user0"
},
"updated_at": "2018-09-18T01:12:26.383Z",
"created_at": "2018-09-18T01:12:26.383Z",
"project_id": null,
"web_url": "http://example.com/snippets/42",
"raw_url": "http://example.com/snippets/42/raw"
},
{
"id": 41,
"title": "Ut praesentium non et atque.",
"file_name": "ondrickaemard.rb",
"description": null,
"visibility": "internal",
"imported": false,
"imported_from": "none",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://example.com/user0"
},
"updated_at": "2018-09-18T01:12:26.360Z",
"created_at": "2018-09-18T01:12:26.360Z",
"project_id": 1,
"web_url": "http://example.com/gitlab-org/gitlab-test/snippets/41",
"raw_url": "http://example.com/gitlab-org/gitlab-test/snippets/41/raw"
}
]获取单个代码片段
获取单个代码片段。
GET /snippets/:id参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 要检索的代码片段 ID。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/1"示例响应:
{
"id": 1,
"title": "test",
"file_name": "add.rb",
"description": "Ruby test snippet",
"visibility": "private",
"imported": false,
"imported_from": "none",
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw"
}单个代码片段内容
获取单个代码片段的原始内容。
GET /snippets/:id/raw参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 要检索的代码片段 ID。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/1/raw"示例响应:
Hello World snippet代码片段仓库文件内容
以纯文本形式返回原始文件内容。
GET /snippets/:id/files/:ref/:file_path/raw参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 要检索的代码片段 ID。 |
ref |
string | 是 | 标签、分支或提交的引用。 |
file_path |
string | 是 | 文件的 URL 编码路径。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/1/files/main/snippet%2Erb/raw"示例响应:
Hello World snippet创建新代码片段
创建新代码片段。
用户必须有权创建新代码片段。
POST /snippets参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
title |
string | 是 | 代码片段标题 |
file_name |
string | 否 | 已弃用:请使用 files。代码片段文件名 |
content |
string | 否 | 已弃用:请使用 files。代码片段内容 |
description |
string | 否 | 代码片段描述 |
visibility |
string | 否 | 代码片段的 可见性 |
files |
数组哈希 | 否 | 代码片段文件数组 |
files:file_path |
string | 是 | 代码片段文件的文件路径 |
files:content |
string | 是 | 代码片段文件的内容 |
示例请求:
curl --request POST "https://gitlab.example.com/api/v4/snippets" \
--header 'Content-Type: application/json' \
--header "PRIVATE-TOKEN: <your_access_token>" \
-d @snippet.json上述示例请求中使用的 snippet.json:
{
"title": "This is a snippet",
"description": "Hello World snippet",
"visibility": "internal",
"files": [
{
"content": "Hello world",
"file_path": "test.txt"
}
]
}示例响应:
{
"id": 1,
"title": "This is a snippet",
"description": "Hello World snippet",
"visibility": "internal",
"imported": false,
"imported_from": "none",
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "test.txt",
"files": [
{
"path": "text.txt",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/main/renamed.md"
}
]
}更新代码片段
更新现有代码片段。
用户必须有权更改现有代码片段。
PUT /snippets/:id参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 要更新的代码片段 ID |
title |
string | 否 | 代码片段标题 |
file_name |
string | 否 | 已弃用:请使用 files。代码片段文件名 |
content |
string | 否 | 已弃用:请使用 files。代码片段内容 |
description |
string | 否 | 代码片段描述 |
visibility |
string | 否 | 代码片段的 可见性 |
files |
数组哈希 | 有时 | 代码片段文件数组。更新包含多个文件的代码片段时必需。 |
files:action |
string | 是 | 对文件执行的操作类型,可选值:create、update、delete、move |
files:file_path |
string | 否 | 代码片段文件的文件路径 |
files:previous_path |
string | 否 | 代码片段文件的先前路径 |
files:content |
string | 否 | 代码片段文件的内容 |
示例请求:
curl --request PUT "https://gitlab.example.com/api/v4/snippets/1" \
--header 'Content-Type: application/json' \
--header "PRIVATE-TOKEN: <your_access_token>" \
-d @snippet.json上述示例请求中使用的 snippet.json:
{
"title": "foo",
"files": [
{
"action": "move",
"previous_path": "test.txt",
"file_path": "renamed.md"
}
]
}示例响应:
{
"id": 1,
"title": "test",
"description": "description of snippet",
"visibility": "internal",
"imported": false,
"imported_from": "none",
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "renamed.md",
"files": [
{
"path": "renamed.md",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/main/renamed.md"
}
]
}删除代码片段
删除现有代码片段。
DELETE /snippets/:id参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 要删除的代码片段 ID。 |
示例请求:
curl --request DELETE \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/1"可能的返回代码:
| 代码 | 描述 |
|---|---|
204 |
删除成功。不返回数据。 |
404 |
未找到代码片段。 |
列出所有公开代码片段
列出所有公开代码片段。
GET /snippets/public参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
per_page |
integer | 否 | 每页返回的代码片段数量。 |
page |
integer | 否 | 要检索的页码。 |
created_after |
datetime | 否 | 返回指定时间之后创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z) |
created_before |
datetime | 否 | 返回指定时间之前创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z) |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1"示例响应:
[
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"id": 12,
"name": "Libby Rolfson",
"state": "active",
"username": "elton_wehner",
"web_url": "http://example.com/elton_wehner"
},
"created_at": "2016-11-25T16:53:34.504Z",
"file_name": "oconnerrice.rb",
"id": 49,
"title": "Ratione cupiditate et laborum temporibus.",
"updated_at": "2016-11-25T16:53:34.504Z",
"project_id": null,
"web_url": "http://example.com/snippets/49",
"raw_url": "http://example.com/snippets/49/raw"
},
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon",
"id": 16,
"name": "Llewellyn Flatley",
"state": "active",
"username": "adaline",
"web_url": "http://example.com/adaline"
},
"created_at": "2016-11-25T16:53:34.479Z",
"file_name": "muellershields.rb",
"id": 48,
"title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
"updated_at": "2016-11-25T16:53:34.479Z",
"project_id": null,
"web_url": "http://example.com/snippets/48",
"raw_url": "http://example.com/snippets/49/raw",
"visibility": "public"
}
]列出所有代码片段
列出当前用户有权访问的所有代码片段。 具有管理员或审计员访问权限的用户可以看到所有代码片段 (个人和项目代码片段)。
GET /snippets/all参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
created_after |
datetime | 否 | 返回指定时间之后创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z)。 |
created_before |
datetime | 否 | 返回指定时间之前创建的代码片段。预期格式为 ISO 8601(2019-03-15T08:00:00Z)。 |
page |
integer | 否 | 要检索的页码。 |
per_page |
integer | 否 | 每页返回的代码片段数量。 |
repository_storage |
string | 否 | 按代码片段使用的仓库存储筛选(仅限管理员)。在 GitLab 16.3 中引入 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/all?per_page=2&page=1"示例响应:
[
{
"id": 113,
"title": "Internal Project Snippet",
"description": null,
"visibility": "internal",
"imported": false,
"imported_from": "none",
"author": {
"id": 17,
"username": "tim_kreiger",
"name": "Tim Kreiger",
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"web_url": "http://example.com/tim_kreiger"
},
"created_at": "2023-08-03T10:21:02.480Z",
"updated_at": "2023-08-03T10:21:02.480Z",
"project_id": 35,
"web_url": "http://example.com/tim_kreiger/internal_project/-/snippets/113",
"raw_url": "http://example.com/tim_kreiger/internal_project/-/snippets/113/raw",
"file_name": "",
"files": [],
"repository_storage": "default"
},
{
"id": 112,
"title": "Private Personal Snippet",
"description": null,
"visibility": "private",
"imported": false,
"imported_from": "none",
"author": {
"id": 1,
"username": "root",
"name": "Administrator",
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"web_url": "http://example.com/root"
},
"created_at": "2023-08-03T10:20:59.994Z",
"updated_at": "2023-08-03T10:20:59.994Z",
"project_id": null,
"web_url": "http://example.com/-/snippets/112",
"raw_url": "http://example.com/-/snippets/112/raw",
"file_name": "",
"files": [],
"repository_storage": "default"
},
{
"id": 111,
"title": "Public Personal Snippet",
"description": null,
"visibility": "public",
"imported": false,
"imported_from": "none",
"author": {
"id": 17,
"username": "tim_kreiger",
"name": "Tim Kreiger",
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"web_url": "http://example.com/tim_kreiger"
},
"created_at": "2023-08-03T10:21:01.312Z",
"updated_at": "2023-08-03T10:21:01.312Z",
"project_id": null,
"web_url": "http://example.com/-/snippets/111",
"raw_url": "http://example.com/-/snippets/111/raw",
"file_name": "",
"files": [],
"repository_storage": "default"
},
]获取用户代理详情
仅对管理员可用。
GET /snippets/:id/user_agent_detail| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer | 是 | 代码片段 ID。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/snippets/1/user_agent_detail"示例响应:
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}