2020-07-30 08:09:33 -04:00
---
stage: Create
group: Source Code
2021-12-28 16:16:11 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-07-30 08:09:33 -04:00
---
2021-02-09 13:09:59 -05:00
# Project badges API **(FREE)**
2018-03-05 12:51:40 -05:00
## Placeholder tokens
2021-11-22 13:10:55 -05:00
Badges support placeholders that are replaced in real-time in both the link and image URL. The allowed placeholders are:
2018-03-05 12:51:40 -05:00
2021-01-22 19:08:46 -05:00
<!-- vale gitlab.Spelling = NO -->
2021-03-08 13:09:12 -05:00
- **%{project_path}**: Replaced by the project path.
- **%{project_id}**: Replaced by the project ID.
- **%{default_branch}**: Replaced by the project default branch.
- **%{commit_sha}**: Replaced by the last project's commit SHA.
2018-03-05 12:51:40 -05:00
2021-01-22 19:08:46 -05:00
<!-- vale gitlab.Spelling = YES -->
2021-08-16 20:10:22 -04:00
2018-03-05 12:51:40 -05:00
## List all badges of a project
Gets a list of a project's badges and its group badges.
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
GET /projects/:id/badges
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2019-11-22 04:06:20 -05:00
| `name` | string | no | Name of the badges to return (case-sensitive). |
2018-03-05 12:51:40 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/:id/badges"
2018-03-05 12:51:40 -05:00
```
Example response:
```json
[
{
2019-11-22 04:06:20 -05:00
"name": "Coverage",
2018-03-05 12:51:40 -05:00
"id": 1,
"link_url": "http://example.com/ci_status.svg?project=%{project_path}& ref=%{default_branch}",
"image_url": "https://shields.io/my/badge",
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project& ref=master",
"rendered_image_url": "https://shields.io/my/badge",
"kind": "project"
},
{
2019-11-22 04:06:20 -05:00
"name": "Pipeline",
2018-03-05 12:51:40 -05:00
"id": 2,
"link_url": "http://example.com/ci_status.svg?project=%{project_path}& ref=%{default_branch}",
"image_url": "https://shields.io/my/badge",
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project& ref=master",
"rendered_image_url": "https://shields.io/my/badge",
"kind": "group"
2021-04-29 08:09:58 -04:00
}
2018-03-05 12:51:40 -05:00
]
```
## Get a badge of a project
Gets a badge of a project.
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
GET /projects/:id/badges/:badge_id
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2018-03-05 12:51:40 -05:00
| `badge_id` | integer | yes | The badge ID |
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/:id/badges/:badge_id"
2018-03-05 12:51:40 -05:00
```
Example response:
```json
{
"id": 1,
"link_url": "http://example.com/ci_status.svg?project=%{project_path}& ref=%{default_branch}",
"image_url": "https://shields.io/my/badge",
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project& ref=master",
"rendered_image_url": "https://shields.io/my/badge",
"kind": "project"
}
```
## Add a badge to a project
Adds a badge to a project.
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
POST /projects/:id/badges
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2018-03-05 12:51:40 -05:00
| `link_url` | string | yes | URL of the badge link |
| `image_url` | string | yes | URL of the badge image |
2021-02-03 19:09:18 -05:00
| `name` | string | no | Name of the badge |
2018-03-05 12:51:40 -05:00
2020-01-30 10:09:15 -05:00
```shell
2021-06-02 11:09:59 -04:00
curl --request POST --header "PRIVATE-TOKEN: < your_access_token > " \
--data "link_url=https://gitlab.com/gitlab-org/gitlab-foss/commits/master& image_url=https://shields.io/my/badge1& name=mybadge" \
"https://gitlab.example.com/api/v4/projects/:id/badges"
2018-03-05 12:51:40 -05:00
```
Example response:
```json
{
"id": 1,
2021-02-03 19:09:18 -05:00
"name": "mybadge",
2019-09-18 10:02:45 -04:00
"link_url": "https://gitlab.com/gitlab-org/gitlab-foss/commits/master",
2018-03-05 12:51:40 -05:00
"image_url": "https://shields.io/my/badge1",
2019-09-18 10:02:45 -04:00
"rendered_link_url": "https://gitlab.com/gitlab-org/gitlab-foss/commits/master",
2018-03-05 12:51:40 -05:00
"rendered_image_url": "https://shields.io/my/badge1",
"kind": "project"
}
```
## Edit a badge of a project
Updates a badge of a project.
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
PUT /projects/:id/badges/:badge_id
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2018-03-05 12:51:40 -05:00
| `badge_id` | integer | yes | The badge ID |
| `link_url` | string | no | URL of the badge link |
| `image_url` | string | no | URL of the badge image |
2021-02-03 19:09:18 -05:00
| `name` | string | no | Name of the badge |
2018-03-05 12:51:40 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --request PUT --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/:id/badges/:badge_id"
2018-03-05 12:51:40 -05:00
```
Example response:
```json
{
"id": 1,
2021-02-03 19:09:18 -05:00
"name": "mybadge",
2019-09-18 10:02:45 -04:00
"link_url": "https://gitlab.com/gitlab-org/gitlab-foss/commits/master",
2018-03-05 12:51:40 -05:00
"image_url": "https://shields.io/my/badge",
2019-09-18 10:02:45 -04:00
"rendered_link_url": "https://gitlab.com/gitlab-org/gitlab-foss/commits/master",
2018-03-05 12:51:40 -05:00
"rendered_image_url": "https://shields.io/my/badge",
"kind": "project"
}
```
## Remove a badge from a project
2021-03-08 13:09:12 -05:00
Removes a badge from a project. Only project badges are removed by using this endpoint.
2018-03-05 12:51:40 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
DELETE /projects/:id/badges/:badge_id
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2018-03-05 12:51:40 -05:00
| `badge_id` | integer | yes | The badge ID |
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --request DELETE --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/:id/badges/:badge_id"
2018-03-05 12:51:40 -05:00
```
## Preview a badge from a project
Returns how the `link_url` and `image_url` final URLs would be after resolving the placeholder interpolation.
2020-02-27 19:09:08 -05:00
```plaintext
2018-03-05 12:51:40 -05:00
GET /projects/:id/badges/render
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
2021-06-28 11:08:03 -04:00
| `id` | integer/string | yes | The ID or [URL-encoded path of the project ](index.md#namespaced-path-encoding ) owned by the authenticated user |
2018-03-05 12:51:40 -05:00
| `link_url` | string | yes | URL of the badge link|
| `image_url` | string | yes | URL of the badge image |
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/projects/:id/badges/render?link_url=http%3A%2F%2Fexample.com%2Fci_status.svg%3Fproject%3D%25%7Bproject_path%7D%26ref%3D%25%7Bdefault_branch%7D& image_url=https%3A%2F%2Fshields.io%2Fmy%2Fbadge"
2018-03-05 12:51:40 -05:00
```
Example response:
```json
2018-04-08 07:39:35 -04:00
{
2018-03-05 12:51:40 -05:00
"link_url": "http://example.com/ci_status.svg?project=%{project_path}& ref=%{default_branch}",
"image_url": "https://shields.io/my/badge",
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project& ref=master",
2021-04-29 08:09:58 -04:00
"rendered_image_url": "https://shields.io/my/badge"
2018-03-05 12:51:40 -05:00
}
```