snippets.md 10.6 KB
Newer Older
1
# Snippets API
2

3
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/6373) in GitLab 8.15.
4 5

Snippets API operates on [snippets](../user/snippets.md).
6

7
## Snippet visibility level
8 9

Snippets in GitLab can be either private, internal, or public.
10
You can set it with the `visibility` field in the snippet.
11

12
Valid values for snippet visibility levels are:
13

14 15 16 17 18
| Visibility | Description                                         |
|:-----------|:----------------------------------------------------|
| `private`  | Snippet is visible only to the snippet creator.     |
| `internal` | Snippet is visible for any logged in user.          |
| `public`   | Snippet can be accessed without any authentication. |
19

20
## List all snippets for a user
21

22
Get a list of the current user's snippets.
23

24
```plaintext
25 26 27
GET /snippets
```

28
Example request:
29

30
```shell
Greg Myers's avatar
Greg Myers committed
31
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets"
32 33 34
```

Example response:
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49
```json
[
    {
        "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",
50
            "web_url": "http://example.com/user0"
51 52 53 54
        },
        "updated_at": "2018-09-18T01:12:26.383Z",
        "created_at": "2018-09-18T01:12:26.383Z",
        "project_id": null,
55 56
        "web_url": "http://example.com/snippets/42",
        "raw_url": "http://example.com/snippets/42/raw"
57 58 59 60 61 62 63 64 65 66 67 68 69
    },
    {
        "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",
70
            "web_url": "http://example.com/user0"
71 72 73
        },
        "updated_at": "2018-09-18T01:12:26.360Z",
        "created_at": "2018-09-18T01:12:26.360Z",
74 75 76
        "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"
77 78
    }
]
79
```
80 81 82 83 84

## Get a single snippet

Get a single snippet.

85
```plaintext
86 87 88 89 90
GET /snippets/:id
```

Parameters:

91 92 93 94 95
| Attribute | Type    | Required | Description                |
|:----------|:--------|:---------|:---------------------------|
| `id`      | integer | yes      | ID of snippet to retrieve. |

Example request:
96

97
```shell
Greg Myers's avatar
Greg Myers committed
98
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
99 100 101 102
```

Example response:

103
```json
104 105 106 107
{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
108
  "description": "Ruby test snippet",
109
  "visibility": "private",
110 111 112 113 114 115 116 117 118 119 120
  "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",
121
  "project_id": null,
122
  "web_url": "http://example.com/snippets/1",
123
  "raw_url": "http://example.com/snippets/1/raw"
124 125 126
}
```

127 128 129 130
## Single snippet contents

Get a single snippet's raw contents.

131
```plaintext
132 133 134 135 136
GET /snippets/:id/raw
```

Parameters:

137 138 139
| Attribute | Type    | Required | Description                |
|:----------|:--------|:---------|:---------------------------|
| `id`      | integer | yes      | ID of snippet to retrieve. |
140

141 142
Example request:

143
```shell
Greg Myers's avatar
Greg Myers committed
144
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/raw"
145 146 147 148
```

Example response:

149
```plaintext
150 151 152
Hello World snippet
```

153 154
## Create new snippet

155
Create a new snippet.
156

157 158 159
NOTE: **Note:**
The user must have permission to create new snippets.

160
```plaintext
161 162 163 164 165
POST /snippets
```

Parameters:

166 167 168 169
| Attribute     | Type   | Required | Description                                        |
|:--------------|:-------|:---------|:---------------------------------------------------|
| `title`       | string | yes      | Title of a snippet.                                |
| `file_name`   | string | yes      | Name of a snippet file.                            |
170
| `content`     | string | yes      | Content of a snippet.                              |
171
| `description` | string | no       | Description of a snippet.                          |
172
| `visibility`  | string | no       | Snippet's [visibility](#snippet-visibility-level). |
173

174
Example request:
175

176
```shell
177
curl --request POST \
178
     --data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
179
     --header 'Content-Type: application/json' \
Greg Myers's avatar
Greg Myers committed
180 181
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/snippets"
182 183 184 185
```

Example response:

186
```json
187 188 189 190
{
  "id": 1,
  "title": "This is a snippet",
  "file_name": "test.txt",
191
  "description": "Hello World snippet",
192
  "visibility": "internal",
193 194 195 196 197 198 199 200 201 202 203
  "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",
204
  "project_id": null,
205
  "web_url": "http://example.com/snippets/1",
206
  "raw_url": "http://example.com/snippets/1/raw"
207 208 209 210 211
}
```

## Update snippet

212
Update an existing snippet.
213

214 215 216
NOTE: **Note:**
The user must have permission to change an existing snippet.

217
```plaintext
218 219 220 221 222
PUT /snippets/:id
```

Parameters:

223 224 225 226 227 228
| Attribute     | Type    | Required | Description                                        |
|:--------------|:--------|:---------|:---------------------------------------------------|
| `id`          | integer | yes      | ID of snippet to update.                           |
| `title`       | string  | no       | Title of a snippet.                                |
| `file_name`   | string  | no       | Name of a snippet file.                            |
| `description` | string  | no       | Description of a snippet.                          |
229
| `content`     | string  | no       | Content of a snippet.                              |
230
| `visibility`  | string  | no       | Snippet's [visibility](#snippet-visibility-level). |
231

232
Example request:
233

234
```shell
235
curl --request PUT \
236
     --data '{"title": "foo", "content": "bar"}' \
237
     --header 'Content-Type: application/json' \
Greg Myers's avatar
Greg Myers committed
238 239
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/snippets/1"
240 241 242 243
```

Example response:

244
```json
245 246 247 248
{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
249
  "description": "description of snippet",
250
  "visibility": "internal",
251 252 253 254 255 256 257 258 259 260 261
  "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",
262
  "project_id": null,
263
  "web_url": "http://example.com/snippets/1",
264
  "raw_url": "http://example.com/snippets/1/raw"
265 266 267 268 269
}
```

## Delete snippet

270
Delete an existing snippet.
271

272
```plaintext
273 274 275 276 277
DELETE /snippets/:id
```

Parameters:

278 279 280
| Attribute | Type    | Required | Description              |
|:----------|:--------|:---------|:-------------------------|
| `id`      | integer | yes      | ID of snippet to delete. |
281

282
Example request:
283

284
```shell
285
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
286 287
```

288
The following are possible return codes:
289

290 291 292 293
| Code  | Description                                 |
|:------|:--------------------------------------------|
| `204` | Delete was successful. No data is returned. |
| `404` | The snippet wasn't found.                   |
294

295 296 297 298
## List all public snippets

List all public snippets.

299
```plaintext
300 301 302
GET /snippets/public
```

303 304 305 306 307 308
Parameters:

| Attribute  | Type    | Required | Description                            |
|:-----------|:--------|:---------|:---------------------------------------|
| `per_page` | integer | no       | Number of snippets to return per page. |
| `page`     | integer | no       | Page to retrieve.                      |
309

310 311
Example request:

312
```shell
Greg Myers's avatar
Greg Myers committed
313
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1"
314 315 316 317
```

Example response:

318
```json
319 320 321 322 323 324 325 326
[
    {
        "author": {
            "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
            "id": 12,
            "name": "Libby Rolfson",
            "state": "active",
            "username": "elton_wehner",
327
            "web_url": "http://example.com/elton_wehner"
328 329 330 331 332 333
        },
        "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",
334 335 336
        "project_id": null,
        "web_url": "http://example.com/snippets/49",
        "raw_url": "http://example.com/snippets/49/raw"
337 338 339 340 341 342 343 344
    },
    {
        "author": {
            "avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon",
            "id": 16,
            "name": "Llewellyn Flatley",
            "state": "active",
            "username": "adaline",
345
            "web_url": "http://example.com/adaline"
346 347 348 349 350 351
        },
        "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",
352 353 354
        "project_id": null,
        "web_url": "http://example.com/snippets/48",
        "raw_url": "http://example.com/snippets/49/raw",
355
        "visibility": "public"
356 357 358
    }
]
```
James Lopez's avatar
James Lopez committed
359 360 361

## Get user agent details

362
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12655) in GitLab 9.4.
James Lopez's avatar
James Lopez committed
363

364 365
NOTE: **Note:**
Available only for administrators.
James Lopez's avatar
James Lopez committed
366

367
```plaintext
James Lopez's avatar
James Lopez committed
368 369 370
GET /snippets/:id/user_agent_detail
```

371 372 373
| Attribute | Type    | Required | Description    |
|:----------|:--------|:---------|:---------------|
| `id`      | integer | yes      | ID of snippet. |
James Lopez's avatar
James Lopez committed
374

375 376
Example request:

377
```shell
Greg Myers's avatar
Greg Myers committed
378
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/user_agent_detail"
James Lopez's avatar
James Lopez committed
379 380 381 382 383 384 385 386
```

Example response:

```json
{
  "user_agent": "AppleWebKit/537.36",
  "ip_address": "127.0.0.1",
387
  "akismet_submitted": false
James Lopez's avatar
James Lopez committed
388 389
}
```