1
1
# Snippets API
2
2
3
- > [ Introduced] [ ce-6373 ] in GitLab 8.15.
3
+ > [ Introduced] ( https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6373 ) in GitLab 8.15.
4
+
5
+ Snippets API operates on [ snippets] ( ../user/snippets.md ) .
4
6
5
7
## Snippet visibility level
6
8
7
9
Snippets in GitLab can be either private, internal, or public.
8
10
You can set it with the ` visibility ` field in the snippet.
9
11
10
- Constants for snippet visibility levels are:
12
+ Valid values for snippet visibility levels are:
11
13
12
- | Visibility | Description |
13
- | ---------- | ----------- |
14
- | ` private ` | The snippet is visible only to the snippet creator |
15
- | ` internal ` | The snippet is visible for any logged in user |
16
- | ` public ` | The snippet can be accessed without any authentication |
14
+ | Visibility | Description |
15
+ | : ----------- | : ---------------------------------------------------- |
16
+ | ` private ` | Snippet is visible only to the snippet creator. |
17
+ | ` internal ` | Snippet is visible for any logged in user. |
18
+ | ` public ` | Snippet can be accessed without any authentication. |
17
19
18
- ## List snippets
20
+ ## List all snippets for a user
19
21
20
- Get a list of current user's snippets.
22
+ Get a list of the current user's snippets.
21
23
22
- ```
24
+ ``` text
23
25
GET /snippets
24
26
```
25
27
26
- ## Single snippet
28
+ Example request:
27
29
28
- Get a single snippet.
30
+ ``` sh
31
+ curl --header " PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets
32
+ ```
33
+
34
+ Example response:
29
35
36
+ ``` json
37
+ [
38
+ {
39
+ "id" : 42 ,
40
+ "title" : " Voluptatem iure ut qui aut et consequatur quaerat." ,
41
+ "file_name" : " mclaughlin.rb" ,
42
+ "description" : null ,
43
+ "visibility" : " internal" ,
44
+ "author" : {
45
+ "id" : 22 ,
46
+ "name" : " User 0" ,
47
+ "username" : " user0" ,
48
+ "state" : " active" ,
49
+ "avatar_url" : " https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon" ,
50
+ "web_url" : " http://localhost:3000/user0"
51
+ },
52
+ "updated_at" : " 2018-09-18T01:12:26.383Z" ,
53
+ "created_at" : " 2018-09-18T01:12:26.383Z" ,
54
+ "project_id" : null ,
55
+ "web_url" : " http://localhost:3000/snippets/42" ,
56
+ "raw_url" : " http://localhost:3000/snippets/42/raw"
57
+ },
58
+ {
59
+ "id" : 41 ,
60
+ "title" : " Ut praesentium non et atque." ,
61
+ "file_name" : " ondrickaemard.rb" ,
62
+ "description" : null ,
63
+ "visibility" : " internal" ,
64
+ "author" : {
65
+ "id" : 22 ,
66
+ "name" : " User 0" ,
67
+ "username" : " user0" ,
68
+ "state" : " active" ,
69
+ "avatar_url" : " https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon" ,
70
+ "web_url" : " http://localhost:3000/user0"
71
+ },
72
+ "updated_at" : " 2018-09-18T01:12:26.360Z" ,
73
+ "created_at" : " 2018-09-18T01:12:26.360Z" ,
74
+ "project_id" : null ,
75
+ "web_url" : " http://localhost:3000/snippets/41" ,
76
+ "raw_url" : " http://localhost:3000/snippets/41/raw"
77
+ }
78
+ ]
30
79
```
80
+
81
+ ## Get a single snippet
82
+
83
+ Get a single snippet.
84
+
85
+ ``` text
31
86
GET /snippets/:id
32
87
```
33
88
34
89
Parameters:
35
90
36
- | Attribute | Type | Required | Description |
37
- | --------- | ---- | -------- | ----------- |
38
- | ` id ` | Integer | yes | The ID of a snippet |
91
+ | Attribute | Type | Required | Description |
92
+ | :----------| :--------| :---------| :---------------------------|
93
+ | ` id ` | integer | yes | ID of snippet to retrieve. |
94
+
95
+ Example request:
39
96
40
- ``` bash
97
+ ``` sh
41
98
curl --header " PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1
42
99
```
43
100
@@ -69,46 +126,52 @@ Example response:
69
126
70
127
Get a single snippet's raw contents.
71
128
72
- ```
129
+ ``` text
73
130
GET /snippets/:id/raw
74
131
```
75
132
76
133
Parameters:
77
134
78
- | Attribute | Type | Required | Description |
79
- | --------- | ---- | -------- | ----------- |
80
- | ` id ` | Integer | yes | The ID of a snippet |
135
+ | Attribute | Type | Required | Description |
136
+ | : ---------- | : -------- | : --------- | : --------------------------- |
137
+ | ` id ` | integer | yes | ID of snippet to retrieve. |
81
138
82
- ``` bash
139
+ Example request:
140
+
141
+ ``` sh
83
142
curl --header " PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/raw
84
143
```
85
144
86
145
Example response:
87
146
88
- ```
147
+ ``` text
89
148
Hello World snippet
90
149
```
91
150
92
151
## Create new snippet
93
152
94
- Creates a new snippet. The user must have permission to create new snippets .
153
+ Create a new snippet.
95
154
96
- ```
155
+ NOTE: ** Note:**
156
+ The user must have permission to create new snippets.
157
+
158
+ ``` text
97
159
POST /snippets
98
160
```
99
161
100
162
Parameters:
101
163
102
- | Attribute | Type | Required | Description |
103
- | --------- | ---- | -------- | ----------- |
104
- | ` title ` | String | yes | The title of a snippet |
105
- | ` file_name ` | String | yes | The name of a snippet file |
106
- | ` content ` | String | yes | The content of a snippet |
107
- | ` description ` | String | no | The description of a snippet |
108
- | ` visibility ` | String | no | The snippet 's visibility |
164
+ | Attribute | Type | Required | Description |
165
+ | : -------------- | : ------- | : --------- | : --------------------------------------------------- |
166
+ | ` title ` | string | yes | Title of a snippet. |
167
+ | ` file_name ` | string | yes | Name of a snippet file. |
168
+ | ` content ` | string | yes | Content of a snippet. |
169
+ | ` description ` | string | no | Description of a snippet. |
170
+ | ` visibility ` | string | no | Snippet 's [ visibility] ( #snippet-visibility-level ) . |
109
171
172
+ Example request:
110
173
111
- ``` bash
174
+ ``` sh
112
175
curl --request POST \
113
176
--data ' {"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
114
177
--header ' Content-Type: application/json' \
@@ -142,25 +205,29 @@ Example response:
142
205
143
206
## Update snippet
144
207
145
- Updates an existing snippet. The user must have permission to change an existing snippet.
208
+ Update an existing snippet.
146
209
147
- ```
210
+ NOTE: ** Note:**
211
+ The user must have permission to change an existing snippet.
212
+
213
+ ``` text
148
214
PUT /snippets/:id
149
215
```
150
216
151
217
Parameters:
152
218
153
- | Attribute | Type | Required | Description |
154
- | --------- | ---- | -------- | ----------- |
155
- | ` id ` | Integer | yes | The ID of a snippet |
156
- | ` title ` | String | no | The title of a snippet |
157
- | ` file_name ` | String | no | The name of a snippet file |
158
- | ` description ` | String | no | The description of a snippet |
159
- | ` content ` | String | no | The content of a snippet |
160
- | ` visibility ` | String | no | The snippet 's visibility |
219
+ | Attribute | Type | Required | Description |
220
+ | : -------------- | : -------- | : --------- | : --------------------------------------------------- |
221
+ | ` id ` | integer | yes | ID of snippet to update. |
222
+ | ` title ` | string | no | Title of a snippet. |
223
+ | ` file_name ` | string | no | Name of a snippet file. |
224
+ | ` description ` | string | no | Description of a snippet. |
225
+ | ` content ` | string | no | Content of a snippet. |
226
+ | ` visibility ` | string | no | Snippet 's [ visibility] ( #snippet-visibility-level ) . |
161
227
228
+ Example request:
162
229
163
- ``` bash
230
+ ``` sh
164
231
curl --request PUT \
165
232
--data ' {"title": "foo", "content": "bar"}' \
166
233
--header ' Content-Type: application/json' \
@@ -194,38 +261,49 @@ Example response:
194
261
195
262
## Delete snippet
196
263
197
- Deletes an existing snippet.
264
+ Delete an existing snippet.
198
265
199
- ```
266
+ ``` text
200
267
DELETE /snippets/:id
201
268
```
202
269
203
270
Parameters:
204
271
205
- | Attribute | Type | Required | Description |
206
- | --------- | ---- | -------- | ----------- |
207
- | ` id ` | Integer | yes | The ID of a snippet |
272
+ | Attribute | Type | Required | Description |
273
+ | : ---------- | : -------- | : --------- | : ------------------------- |
274
+ | ` id ` | integer | yes | ID of snippet to delete. |
208
275
276
+ Example request:
209
277
210
- ```
278
+ ``` sh
211
279
curl --request DELETE --header " PRIVATE-TOKEN: <your_access_token>" " https://gitlab.example.com/api/v4/snippets/1"
212
280
```
213
281
214
- upon successful delete a ` 204 No content ` HTTP code shall be expected, with no data,
215
- but if the snippet is non-existent, a ` 404 Not Found ` will be returned.
282
+ The following are possible return codes:
216
283
217
- ## Explore all public snippets
284
+ | Code | Description |
285
+ | :------| :--------------------------------------------|
286
+ | ` 204 ` | Delete was successful. No data is returned. |
287
+ | ` 404 ` | The snippet wasn't found. |
218
288
219
- ```
289
+ ## List all public snippets
290
+
291
+ List all public snippets.
292
+
293
+ ``` text
220
294
GET /snippets/public
221
295
```
222
296
223
- | Attribute | Type | Required | Description |
224
- | --------- | ---- | -------- | ----------- |
225
- | ` per_page ` | Integer | no | number of snippets to return per page |
226
- | ` page ` | Integer | no | the page to retrieve |
297
+ Parameters:
298
+
299
+ | Attribute | Type | Required | Description |
300
+ | :-----------| :--------| :---------| :---------------------------------------|
301
+ | ` per_page ` | integer | no | Number of snippets to return per page. |
302
+ | ` page ` | integer | no | Page to retrieve. |
227
303
228
- ``` bash
304
+ Example request:
305
+
306
+ ``` sh
229
307
curl --header " PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/public? per_page=2& page=1
230
308
```
231
309
@@ -273,21 +351,22 @@ Example response:
273
351
274
352
## Get user agent details
275
353
276
- > ** Notes:**
277
- > [ Introduced] [ ce-29508 ] in GitLab 9.4.
278
-
354
+ > [ Introduced] ( https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12655 ) in GitLab 9.4.
279
355
280
- Available only for admins.
356
+ NOTE: ** Note:**
357
+ Available only for administrators.
281
358
282
- ```
359
+ ``` text
283
360
GET /snippets/:id/user_agent_detail
284
361
```
285
362
286
- | Attribute | Type | Required | Description |
287
- | ------------- | --------- | ---------- | ----------------------- ---------------|
288
- | ` id ` | Integer | yes | The ID of a snippet |
363
+ | Attribute | Type | Required | Description |
364
+ | : ----------| : --------| : ---------| : ---------------|
365
+ | ` id ` | integer | yes | ID of snippet. |
289
366
290
- ``` bash
367
+ Example request:
368
+
369
+ ``` sh
291
370
curl --request GET --header " PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/user_agent_detail
292
371
```
293
372
@@ -300,6 +379,3 @@ Example response:
300
379
"akismet_submitted" : false
301
380
}
302
381
```
303
-
304
- [ ce-6373 ] : https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6373
305
- [ ce-29508 ] : https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12655
0 commit comments