正式なドキュメントは英語版であり、この日本語訳はAI支援翻訳により作成された参考用のものです。日本語訳の一部の内容は人間によるレビューがまだ行われていないため、翻訳のタイミングにより英語版との間に差異が生じることがあります。最新かつ正確な情報については、英語版をご参照ください。

スニペットAPI

  • プラン: Free、Premium、Ultimate
  • 提供形態: GitLab.com、GitLab Self-Managed、GitLab Dedicated

スニペットAPIは、スニペットを操作します。関連APIは、プロジェクトスニペットストレージ間でのスニペットの移動のために存在します。

スニペットの表示レベル

GitLabのスニペットは、private、internal、publicのいずれかにすることができます。スニペットのvisibilityフィールドで設定できます。

スニペットの表示レベルの有効な値は次のとおりです:

表示レベル:説明
privateスニペットはスニペットの作成者のみに表示されます。
internalスニペットは、外部ユーザーを除く認証済みユーザーであれば誰でも表示できます。
publicスニペットには認証なしでアクセスできます。

現在のユーザーのスニペットをすべてリスト表示

現在のユーザーのスニペットのリストを取得します。

GET /snippets

サポートされている属性は以下のとおりです:

属性必須説明
created_after日時いいえ指定された時間以降に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
created_before日時いいえ指定された時間より前に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
page整数いいえ取得するページ。
per_page整数いいえページごとに返すスニペットの数。

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
file_name文字列スニペットファイルの名前。
id整数スニペットのID。
importedブール値trueの場合、スニペットはインポートされました。
imported_from文字列インポート元。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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整数はい取得するスニペットのID。

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
expires_at文字列スニペットの有効期限が切れる日時。
file_name文字列スニペットファイルの名前。
http_url_to_repo文字列スニペットリポジトリのHTTP URL。
id整数スニペットのID。
importedブール値trueの場合、スニペットはインポートされました。
imported_from文字列インポート元。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
ssh_url_to_repo文字列スニペットリポジトリのSSH URL。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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

単一スニペットコンテンツ

単一スニペットのrawコンテンツを取得します。

GET /snippets/:id/raw

サポートされている属性は以下のとおりです:

属性必須説明
id整数はい取得するスニペットのID。

成功した場合、200 OKとスニペットのrawコンテンツを返します。

リクエスト例:

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

レスポンス例:

Hello World snippet

スニペットリポジトリファイルコンテンツ

rawファイルコンテンツをプレーンテキストとして返します。

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

サポートされている属性は以下のとおりです:

属性必須説明
file_path文字列はいファイルへのURLエンコードされたパス。
id整数はい取得するスニペットのID。
ref文字列はいタグ、ブランチ、またはコミットへの参照。

成功した場合、200 OKとrawファイルコンテンツを返します。

リクエスト例:

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

サポートされている属性は以下のとおりです:

属性必須説明
files:content文字列はいスニペットファイルのコンテンツ。
files:file_path文字列はいスニペットファイルのファイルパス。
title文字列はいスニペットのタイトル。
content文字列いいえ非推奨: 代わりにfilesを使用してください。スニペットのコンテンツ。
description文字列いいえスニペットの説明。
file_name文字列いいえ非推奨: 代わりにfilesを使用してください。スニペットファイルの名前。
filesハッシュの配列いいえスニペットファイルの配列。
visibility文字列いいえスニペットの表示レベル

成功した場合、201 Createdと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
expires_at文字列スニペットの有効期限が切れる日時。
file_name文字列スニペットファイルの名前。
files配列スニペットファイルの配列。
http_url_to_repo文字列スニペットリポジトリのHTTP URL。
id整数スニペットのID。
importedブール値trueの場合、スニペットはインポートされました。
imported_from文字列インポート元。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
ssh_url_to_repo文字列スニペットリポジトリのSSH URL。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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整数はい更新するスニペットのID。
files:action文字列はいファイルに対して実行するアクションの種類。次のいずれか: createupdatedeletemove
content文字列いいえ非推奨: 代わりにfilesを使用してください。スニペットのコンテンツ。
description文字列いいえスニペットの説明。
file_name文字列いいえ非推奨: 代わりにfilesを使用してください。スニペットファイルの名前。
filesハッシュの配列条件付きスニペットファイルの配列。複数のファイルを含むスニペットを更新する場合は必須。
files:content文字列いいえスニペットファイルのコンテンツ。
files:file_path文字列いいえスニペットファイルのファイルパス。
files:previous_path文字列いいえスニペットファイルの以前のパス。
title文字列いいえスニペットのタイトル。
visibility文字列いいえスニペットの表示レベル

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
expires_at文字列スニペットの有効期限が切れる日時。
file_name文字列スニペットファイルの名前。
files配列スニペットファイルの配列。
http_url_to_repo文字列スニペットリポジトリのHTTP URL。
id整数スニペットのID。
importedブール値trueの場合、スニペットはインポートされました。
imported_from文字列インポート元。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
ssh_url_to_repo文字列スニペットリポジトリのSSH URL。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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整数はい削除するスニペットのID。

リクエスト例:

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

考えられる戻り値は次のとおりです:

コード説明
204削除に成功しました。データは返されません。
404スニペットが見つかりませんでした。

すべての公開スニペットをリスト表示

すべての公開スニペットをリスト表示します。

GET /snippets/public

サポートされている属性は以下のとおりです:

属性必須説明
created_after日時いいえ指定された時間以降に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
created_before日時いいえ指定された時間より前に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
page整数いいえ取得するページ。
per_page整数いいえページごとに返すスニペットの数。

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
file_name文字列スニペットファイルの名前。
id整数スニペットのID。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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日時いいえ指定された時間以降に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
created_before日時いいえ指定された時間より前に作成されたスニペットを返します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。
page整数いいえ取得するページ。
per_page整数いいえページごとに返すスニペットの数。
repository_storage文字列いいえスニペットが使用するリポジトリストレージでフィルタリングします_(管理者のみ)_。GitLab 16.3で導入されました。

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
authorオブジェクトスニペットの作成者を表すユーザーオブジェクト。
created_at文字列スニペットが作成された日時。
description文字列スニペットの説明。
file_name文字列スニペットファイルの名前。
files配列スニペットファイルの配列。
id整数スニペットのID。
importedブール値trueの場合、スニペットはインポートされました。
imported_from文字列インポート元。
project_id整数関連付けられたプロジェクトのID。パーソナルスニペットの場合、null
raw_url文字列rawスニペットコンテンツへのURL。
repository_storage文字列スニペットで使用されるリポジトリストレージ。
title文字列スニペットのタイトル。
updated_at文字列スニペットが最後に更新された日時。
visibility文字列スニペットの表示レベル。
web_url文字列GitLab UIのスニペットへのURL。

リクエスト例:

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整数はいスニペットのID。

成功した場合、200 OKと次のレスポンス属性を返します:

属性説明
akismet_submittedブール値trueの場合、詳細はAkismetに送信されました。
ip_address文字列スニペットの作成に使用されたIPアドレス。
user_agent文字列スニペットの作成に使用されたユーザーエージェント文字列。

リクエスト例:

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
}