2018-06-25 07:12:46 -05:00
---
date: "2018-06-24:00:00+02:00"
title: "API Usage"
slug: "api-usage"
weight: 40
2020-12-09 01:47:06 -05:00
toc: false
2018-06-25 07:12:46 -05:00
draft: false
menu:
sidebar:
2020-10-23 10:59:45 -05:00
parent: "developers"
2018-06-25 07:12:46 -05:00
name: "API Usage"
weight: 40
identifier: "api-usage"
---
2020-12-07 23:52:26 -05:00
# API Usage
2020-12-09 01:47:06 -05:00
**Table of Contents**
2020-12-07 23:52:26 -05:00
{{< toc > }}
2018-06-25 07:12:46 -05:00
## Enabling/configuring API access
2019-01-23 15:09:18 -05:00
By default, `ENABLE_SWAGGER` is true, and
2020-12-09 01:47:06 -05:00
`MAX_RESPONSE_ITEMS` is set to 50. See [Config Cheat
2018-06-25 07:12:46 -05:00
Sheet](https://docs.gitea.io/en-us/config-cheat-sheet/) for more
information.
2020-12-07 23:52:26 -05:00
## Authentication
2018-06-25 07:12:46 -05:00
Gitea supports these methods of API authentication:
- HTTP basic authentication
- `token=...` parameter in URL query string
- `access_token=...` parameter in URL query string
- `Authorization: token ...` header in HTTP headers
2020-12-09 01:47:06 -05:00
All of these methods accept the same API key token type. You can
2018-06-25 07:12:46 -05:00
better understand this by looking at the code -- as of this writing,
Gitea parses queries and headers to find the token in
[modules/auth/auth.go ](https://github.com/go-gitea/gitea/blob/6efdcaed86565c91a3dc77631372a9cc45a58e89/modules/auth/auth.go#L47 ).
2019-03-09 16:15:45 -05:00
You can create an API key token via your Gitea installation's web interface:
2018-06-25 07:12:46 -05:00
`Settings | Applications | Generate New Token` .
2020-12-07 23:52:26 -05:00
## OAuth2 Provider
2019-04-14 03:42:11 -05:00
Access tokens obtained from Gitea's [OAuth2 provider ](https://docs.gitea.io/en-us/oauth2-provider ) are accepted by these methods:
- `Authorization bearer ...` header in HTTP headers
- `token=...` parameter in URL query string
- `access_token=...` parameter in URL query string
2018-06-25 07:12:46 -05:00
### More on the `Authorization:` header
For historical reasons, Gitea needs the word `token` included before
2019-03-09 16:15:45 -05:00
the API key token in an authorization header, like this:
2018-06-25 07:12:46 -05:00
2020-12-09 01:47:06 -05:00
```sh
2018-06-25 07:12:46 -05:00
Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675
```
In a `curl` command, for instance, this would look like:
2020-12-09 01:47:06 -05:00
```sh
2018-06-25 07:12:46 -05:00
curl -X POST "http://localhost:4000/api/v1/repos/test1/test1/issues" \
-H "accept: application/json" \
-H "Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675" \
-H "Content-Type: application/json" -d "{ \"body\": \"testing\", \"title\": \"test 20\"}" -i
```
As mentioned above, the token used is the same one you would use in
the `token=` string in a GET request.
2019-10-10 07:42:01 -05:00
## API Guide:
2020-10-22 12:04:23 -05:00
API Reference guide is auto-generated by swagger and available on:
2020-12-09 01:47:06 -05:00
`https://gitea.your.host/api/swagger`
or on
[gitea demo instance ](https://try.gitea.io/api/swagger )
2019-10-10 07:42:01 -05:00
2018-06-25 07:12:46 -05:00
## Listing your issued tokens via the API
As mentioned in
[#3842 ](https://github.com/go-gitea/gitea/issues/3842#issuecomment-397743346 ),
`/users/:name/tokens` is special and requires you to authenticate
using BasicAuth, as follows:
### Using basic authentication:
2020-12-09 01:47:06 -05:00
```sh
2018-06-25 07:12:46 -05:00
$ curl --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
[{"name":"test","sha1":"..."},{"name":"dev","sha1":"..."}]
```
2018-09-06 22:31:29 -05:00
2019-05-07 15:53:45 -05:00
As of v1.8.0 of Gitea, if using basic authentication with the API and your user has two factor authentication enabled, you'll need to send an additional header that contains the one time password (6 digit rotating token). An example of the header is `X-Gitea-OTP: 123456` where `123456` is where you'd place the code from your authenticator. Here is how the request would look like in curl:
2020-12-09 01:47:06 -05:00
```sh
2019-05-07 15:53:45 -05:00
$ curl -H "X-Gitea-OTP: 123456" --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
```
2018-09-06 22:31:29 -05:00
## Sudo
The API allows admin users to sudo API requests as another user. Simply add either a `sudo=` parameter or `Sudo:` request header with the username of the user to sudo.
2020-10-22 12:04:23 -05:00
## SDKs
2020-12-09 01:47:06 -05:00
- [Official go-sdk ](https://gitea.com/gitea/go-sdk )
- [more ](https://gitea.com/gitea/awesome-gitea#user-content-sdk )