Snippets API

Tier: Free, Premium, Ultimate Offering: GitLab.com, Self-Managed, GitLab Dedicated

Snippets API는 snippets에서 작동합니다. 관련된 API는 프로젝트 스니펫스토리지 간 스니펫 이동을 위해 존재합니다.

스니펫 가시성 수준

GitLab의 스니펫은 비공개, 내부, 또는 공개 중 하나일 수 있습니다. 스니펫의 visibility 필드로 설정할 수 있습니다.

스니펫 가시성 수준의 유효한 값은 다음과 같습니다:

가시성 설명
private 스니펫은 스니펫 생성자에게만 표시됩니다.
internal 스니펫은 외부 사용자를 제외한 모든 인증된 사용자에게 표시됩니다.
public 인증 없이 스니펫에 액세스할 수 있습니다.

현재 사용자의 모든 스니펫 나열

현재 사용자의 스니펫 목록을 가져옵니다.

GET /snippets

매개변수:

속성 유형 필수 설명
per_page 정수 아니요 페이지 당 반환할 스니펫 수.
page 정수 아니요 검색할 페이지.
created_after 날짜/시간 아니요 주어진 시간 이후에 생성된 스니펫 반환. ISO 8601 형식으로 예상됨 (2019-03-15T08:00:00Z)
created_before 날짜/시간 아니요 주어진 시간 이전에 생성된 스니펫 반환. ISO 8601 형식으로 예상됨 (2019-03-15T08:00:00Z)

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "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",
        "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",
        "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"
    }
]

## 단일 스니펫 가져오기

단일 스니펫을 가져옵니다.

```plaintext
GET /snippets/:id

매개변수:

속성 유형 필수 설명
id 정수 검색할 스니펫의 ID.

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"

예시 응답:

{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
  "description": "Ruby test snippet",
  "visibility": "private",
  "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"
}
## 단일 스니펫 내용

단일 스니펫의 로우 내용을 가져옵니다.

```plaintext
GET /snippets/:id/raw

매개변수:

속성 타입 필수 설명
id integer yes 검색할 스니펫의 ID

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/raw"

예시 응답:

Hello World snippet

스니펫 저장소 파일 내용

일반 텍스트로 원시 파일 내용을 반환합니다.

GET /snippets/:id/files/:ref/:file_path/raw

매개변수:

속성 타입 필수 설명
id integer yes 검색할 스니펫의 ID
ref string yes 태그, 브랜치 또는 커밋에 대한 참조
file_path string yes 파일의 URL 인코딩된 경로

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/files/main/snippet%2Erb/raw"

예시 응답:

Hello World snippet

새로운 스니펫 생성

새로운 스니펫을 생성합니다.

참고: 사용자는 새로운 스니펫을 만들 권한이 있어야 합니다.

POST /snippets

매개변수:

속성 타입 필수 설명
title string yes 스니펫의 제목
file_name string no 사용되지 않음: 대신 files 사용. 스니펫 파일의 이름
content string no 사용되지 않음: 대신 files 사용. 스니펫의 내용
description string no 스니펫 설명
visibility string no 스니펫의 가시성
files 해시 배열 no 스니펫 파일의 배열
files:file_path string yes 스니펫 파일의 경로
files:content string yes 스니펫 파일의 내용

예시 요청:

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",
  "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 yes 업데이트할 스니펫의 ID
title string no 스니펫의 제목
file_name string no 사용되지 않음: 대신 files 사용. 스니펫 파일의 이름
content string no 사용되지 않음: 대신 files 사용. 스니펫의 내용
description string no 스니펫 설명
visibility string no 스니펫의 가시성
files 해시 배열 sometimes 스니펫 파일의 배열. 여러 파일을 업데이트하는 경우 필요함.
files:action string yes 파일에 대한 수행할 동작 유형. create, update, delete, move 중 하나
files:file_path string no 스니펫 파일의 경로
files:previous_path string no 스니펫 파일의 이전 경로
files:content string no 스니펫 파일의 내용

예시 요청:

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",
  "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 정수 삭제할 스니펫의 ID입니다.

예시 요청:

curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"

다음은 가능한 반환 코드들입니다:

코드 설명
204 삭제가 성공했습니다. 반환 데이터가 없습니다.
404 스니펫을 찾을 수 없습니다.

모든 공개 스니펫 나열

모든 공개 스니펫을 나열합니다.

GET /snippets/public

매개변수:

속성 유형 필수 설명
per_page 정수 아니오 페이지당 반환할 스니펫 수입니다.
page 정수 아니오 검색할 페이지입니다.
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>" "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 정수 아니오 검색할 페이지입니다.
per_page 정수 아니오 페이지당 반환할 스니펫 수입니다.
repository_storage 문자열 아니오 스니펫에 의해 사용된 저장소 저장소별로 필터링합니다_(관리자 전용)_.GitLab 16.3에서 소개됨

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/all?per_page=2&page=1"

예시 응답:

[
    {
        "id": 113,
        "title": "Internal Project Snippet",
        "description": null,
        "visibility": "internal",
        "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",
        "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",
        "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 yes 스니펫 ID.

예시 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "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
}