|
| 1 | +--- |
| 2 | +date: "2018-06-24:00:00+02:00" |
| 3 | +title: "API Usage" |
| 4 | +slug: "api-usage" |
| 5 | +weight: 40 |
| 6 | +toc: true |
| 7 | +draft: false |
| 8 | +menu: |
| 9 | + sidebar: |
| 10 | + parent: "advanced" |
| 11 | + name: "API Usage" |
| 12 | + weight: 40 |
| 13 | + identifier: "api-usage" |
| 14 | +--- |
| 15 | + |
| 16 | +# Gitea API Usage |
| 17 | + |
| 18 | +## Enabling/configuring API access |
| 19 | + |
| 20 | +By default, `ENABLE_SWAGGER_ENDPOINT` is true, and |
| 21 | +`MAX_RESPONSE_ITEMS` is set to 50. See [Config Cheat |
| 22 | +Sheet](https://docs.gitea.io/en-us/config-cheat-sheet/) for more |
| 23 | +information. |
| 24 | + |
| 25 | +## Authentication via the API |
| 26 | + |
| 27 | +Gitea supports these methods of API authentication: |
| 28 | + |
| 29 | +- HTTP basic authentication |
| 30 | +- `token=...` parameter in URL query string |
| 31 | +- `access_token=...` parameter in URL query string |
| 32 | +- `Authorization: token ...` header in HTTP headers |
| 33 | + |
| 34 | +All of these methods accept the same apiKey token type. You can |
| 35 | +better understand this by looking at the code -- as of this writing, |
| 36 | +Gitea parses queries and headers to find the token in |
| 37 | +[modules/auth/auth.go](https://github.com/go-gitea/gitea/blob/6efdcaed86565c91a3dc77631372a9cc45a58e89/modules/auth/auth.go#L47). |
| 38 | + |
| 39 | +You can create an apiKey token via your gitea install's web interface: |
| 40 | +`Settings | Applications | Generate New Token`. |
| 41 | + |
| 42 | +### More on the `Authorization:` header |
| 43 | + |
| 44 | +For historical reasons, Gitea needs the word `token` included before |
| 45 | +the apiKey token in an authorization header, like this: |
| 46 | + |
| 47 | +``` |
| 48 | +Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675 |
| 49 | +``` |
| 50 | + |
| 51 | +In a `curl` command, for instance, this would look like: |
| 52 | + |
| 53 | +``` |
| 54 | +curl -X POST "http://localhost:4000/api/v1/repos/test1/test1/issues" \ |
| 55 | + -H "accept: application/json" \ |
| 56 | + -H "Authorization: token 65eaa9c8ef52460d22a93307fe0aee76289dc675" \ |
| 57 | + -H "Content-Type: application/json" -d "{ \"body\": \"testing\", \"title\": \"test 20\"}" -i |
| 58 | +``` |
| 59 | + |
| 60 | +As mentioned above, the token used is the same one you would use in |
| 61 | +the `token=` string in a GET request. |
| 62 | + |
| 63 | +## Listing your issued tokens via the API |
| 64 | + |
| 65 | +As mentioned in |
| 66 | +[#3842](https://github.com/go-gitea/gitea/issues/3842#issuecomment-397743346), |
| 67 | +`/users/:name/tokens` is special and requires you to authenticate |
| 68 | +using BasicAuth, as follows: |
| 69 | + |
| 70 | +### Using basic authentication: |
| 71 | + |
| 72 | +``` |
| 73 | +$ curl --request GET --url https://yourusername:[email protected]/api/v1/users/yourusername/tokens |
| 74 | +[{"name":"test","sha1":"..."},{"name":"dev","sha1":"..."}] |
| 75 | +``` |
0 commit comments