스니펫 API

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

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

스니펫 가시성 수준

GitLab의 스니펫은 개인, 내부, 또는 공개일 수 있습니다. 스니펫의 가시성 필드로 설정할 수 있습니다.

스니펫 가시성 수준에 대한 유효한 값은 다음과 같습니다.

가시성 설명
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",
        "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 정수 검색할 스니펫 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",
  "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 정수 검색할 스니펫 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 정수 가져올 스니펫의 ID
ref 문자열 태그, 브랜치 또는 커밋에 대한 참조
file_path 문자열 파일에 대한 URL 인코딩된 경로

예시 요청:

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

예시 응답:

Hello World snippet

새로운 스니펫 생성

새로운 스니펫을 만듭니다.

note
사용자는 새 스니펫을 생성할 수 있는 권한이 필요합니다.
POST /snippets

매개변수:

속성 유형 필수 설명
title 문자열 스니펫 제목
file_name 문자열 아니오 사용되지 않음: 대신 files를 사용하십시오. 스니펫 파일 이름
content 문자열 아니오 사용되지 않음: 대신 files를 사용하십시오. 스니펫 내용
description 문자열 아니오 스니펫 설명
visibility 문자열 아니오 스니펫의 가시성
files 해시의 배열 아니오 스니펫 파일의 배열
files:file_path 문자열 스니펫 파일 경로
files:content 문자열 스니펫 파일 내용

예시 요청:

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"
    }
  ]
}

스니펫 업데이트

기존 스니펫을 업데이트합니다.

note
사용자는 기존 스니펫을 변경할 수 있는 권한이 있어야 합니다.
PUT /snippets/:id

매개변수:

속성 유형 필수 설명
id 정수 업데이트할 스니펫의 ID
title 문자열 아니오 스니펫의 제목
file_name 문자열 아니오 사용되지 않음: 대신 files를 사용하십시오. 스니펫 파일의 이름
content 문자열 아니오 사용되지 않음: 대신 files를 사용하십시오. 스니펫의 내용
description 문자열 아니오 스니펫의 설명
visibility 문자열 아니오 스니펫의 가시성
files 해시 배열 때때로 스니펫 파일의 배열. 여러 파일을 업데이트할 때 필요합니다.
files:action 문자열 파일에 수행할 동작 유형 중 하나: create, update, delete, move
files:file_path 문자열 아니오 스니펫 파일의 경로
files:previous_path 문자열 아니오 스니펫 파일의 이전 경로
files:content 문자열 아니오 스니펫 파일의 내용

예시 요청:

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": "스니펫 설명",
  "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 정수 삭제할 스니펫의 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 일시 아니오 주어진 시간 이후에 생성된 스니펫 반환. 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/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 일시 아니오 주어진 시간 이후에 생성된 스니펫 반환. ISO 8601 형식 예상 (2019-03-15T08:00:00Z).
created_before 일시 아니오 주어진 시간 이전에 생성된 스니펫을 반환. 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": "내부 프로젝트 스니펫",
    "description": null,
    "visibility": "내부",
    "imported": false,
    "imported_from": "없음",
    "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": "기본"
  },
  {
    "id": 112,
    "title": "개인 프로젝트 스니펫",
    "description": null,
    "visibility": "개인",
    "imported": false,
    "imported_from": "없음",
    "author": {
      "id": 1,
      "username": "root",
      "name": "관리자",
      "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": "기본"
  },
  {
    "id": 111,
    "title": "공개 개인 스니펫",
    "description": null,
    "visibility": "공개",
    "imported": false,
    "imported_from": "없음",
    "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": "기본"
  },
]

사용자 에이전트 세부 정보 가져오기

note
관리자만 이용 가능합니다.
GET /snippets/:id/user_agent_detail
속성 유형 필요여부 설명
id 정수 스니펫의 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
}