snippets.md 8.14 KB
Newer Older
1
# Snippets API
2 3 4

> [Introduced][ce-6373] in GitLab 8.15.

5
## Snippet visibility level
6 7

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

Constants for snippet visibility levels are:

12 13 14 15 16
| Visibility | Description |
| ---------- | ----------- |
| `private`  | The snippet is visible only to the snippet creator |
| `internal` | The snippet is visible for any logged in user |
| `public`   | The snippet can be accessed without any authentication |
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

## List snippets

Get a list of current user's snippets.

```
GET /snippets
```

## Single snippet

Get a single snippet.

```
GET /snippets/:id
```

Parameters:

| Attribute          | Type    | Required | Description                   |
| ---------          | ----    | -------- | -----------                   |
| `id`               | Integer | yes      | The ID of a snippet           |

40
```bash
41
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1
42 43 44 45
```

Example response:

46
```json
47 48 49 50
{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
51
  "description": "Ruby test snippet",
52
  "visibility": "private",
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  "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",
  "web_url": "http://example.com/snippets/1",
}
```

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
## Single snippet contents

Get a single snippet's raw contents.

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

Parameters:

| Attribute          | Type    | Required | Description                   |
| ---------          | ----    | -------- | -----------                   |
| `id`               | Integer | yes      | The ID of a snippet           |

```bash
83
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/raw
84 85 86 87 88 89 90 91
```

Example response:

```
Hello World snippet
```

92 93 94 95 96 97 98 99 100 101
## Create new snippet

Creates a new snippet. The user must have permission to create new snippets.

```
POST /snippets
```

Parameters:

102 103 104 105 106 107 108
| Attribute          | Type    | Required | Description                  |
| ---------          | ----    | -------- | -----------                  |
| `title`            | String  | yes      | The title of a snippet       |
| `file_name`        | String  | yes      | The name of a snippet file   |
| `content`          | String  | yes      | The content of a snippet     |
| `description`      | String  | no       | The description of a snippet |
| `visibility`       | String  | no       | The snippet's visibility     |
109 110


111
```bash
112 113 114 115 116
curl --request POST \
     --data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
     --header 'Content-Type: application/json' \
     --header "PRIVATE-TOKEN: valid_api_token" \
     https://gitlab.example.com/api/v4/snippets
117 118 119 120
```

Example response:

121
```json
122 123 124 125
{
  "id": 1,
  "title": "This is a snippet",
  "file_name": "test.txt",
126
  "description": "Hello World snippet",
127
  "visibility": "internal",
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  "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",
  "web_url": "http://example.com/snippets/1",
}
```

## Update snippet

Updates an existing snippet. The user must have permission to change an existing snippet.

```
PUT /snippets/:id
```

Parameters:

153 154 155 156 157 158 159 160
| Attribute          | Type    | Required | Description                  |
| ---------          | ----    | -------- | -----------                  |
| `id`               | Integer | yes      | The ID of a snippet          |
| `title`            | String  | no       | The title of a snippet       |
| `file_name`        | String  | no       | The name of a snippet file   |
| `description`      | String  | no       | The description of a snippet |
| `content`          | String  | no       | The content of a snippet     |
| `visibility`       | String  | no       | The snippet's visibility     |
161 162


163
```bash
164 165 166 167 168
curl --request PUT \
     --data '{"title": "foo", "content": "bar"}' \
     --header 'Content-Type: application/json' \
     --header "PRIVATE-TOKEN: valid_api_token" \
     https://gitlab.example.com/api/v4/snippets/1
169 170 171 172
```

Example response:

173
```json
174 175 176 177
{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
178
  "description": "description of snippet",
179
  "visibility": "internal",
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  "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",
  "web_url": "http://example.com/snippets/1",
}
```

## Delete snippet

197
Deletes an existing snippet.
198 199 200 201 202 203 204 205 206 207 208 209 210

```
DELETE /snippets/:id
```

Parameters:

| Attribute          | Type    | Required | Description                   |
| ---------          | ----    | -------- | -----------                   |
| `id`               | Integer | yes      | The ID of a snippet           |


```
211
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
```

upon successful delete a `204 No content` HTTP code shall be expected, with no data,
but if the snippet is non-existent, a `404 Not Found` will be returned.

## Explore all public snippets

```
GET /snippets/public
```

| Attribute  | Type    | Required | Description                           |
| ---------  | ----    | -------- | -----------                           |
| `per_page` | Integer | no       | number of snippets to return per page |
| `page`     | Integer | no       | the page to retrieve                  |

228
```bash
229
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1
230 231 232 233
```

Example response:

234
```json
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
[
    {
        "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://localhost:3000/elton_wehner"
        },
        "created_at": "2016-11-25T16:53:34.504Z",
        "file_name": "oconnerrice.rb",
        "id": 49,
        "raw_url": "http://localhost:3000/snippets/49/raw",
        "title": "Ratione cupiditate et laborum temporibus.",
        "updated_at": "2016-11-25T16:53:34.504Z",
        "web_url": "http://localhost:3000/snippets/49"
    },
    {
        "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://localhost:3000/adaline"
        },
        "created_at": "2016-11-25T16:53:34.479Z",
        "file_name": "muellershields.rb",
        "id": 48,
        "raw_url": "http://localhost:3000/snippets/48/raw",
        "title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
        "updated_at": "2016-11-25T16:53:34.479Z",
268 269
        "web_url": "http://localhost:3000/snippets/48",
        "visibility": "public"
270 271 272
    }
]
```
James Lopez's avatar
James Lopez committed
273 274 275

## Get user agent details

James Lopez's avatar
James Lopez committed
276 277 278 279
> **Notes:**
> [Introduced][ce-29508] in GitLab 9.4.


James Lopez's avatar
James Lopez committed
280 281
Available only for admins.

James Lopez's avatar
James Lopez committed
282 283 284 285 286 287 288 289 290
```
GET /snippets/:id/user_agent_detail
```

| Attribute   | Type    | Required | Description                          |
|-------------|---------|----------|--------------------------------------|
| `id`        | Integer | yes      | The ID of a snippet                  |

```bash
291
curl --request GET --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/user_agent_detail
James Lopez's avatar
James Lopez committed
292 293 294 295 296 297 298 299
```

Example response:

```json
{
  "user_agent": "AppleWebKit/537.36",
  "ip_address": "127.0.0.1",
300
  "akismet_submitted": false
James Lopez's avatar
James Lopez committed
301 302
}
```
303

304 305
[ce-6373]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6373
[ce-29508]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12655