# API token as header

**URL:** https://discourse.joplinapp.org/t/api-token-as-header/2815
**Category:** Development
**Created:** [28 June 2019 21:18 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815 "2019-06-28T21:18:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tessus](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/tessus/32/13_2.png) [@tessus](https://discourse.joplinapp.org/u/tessus)
#### Post date: [28 June 2019 21:18 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/1 "2019-06-28T21:18:45Z")

</div>

I would like to propose the possibility to send the token via a header.  
Maybe this is just a personal preference, but I always thought that tokens are better sent in the header than in the GET request.

I would change the code in that way that if the token is present in the header, no token is needed in the GET request. The token would still be valid in the GET request though.  
However, we have to decide which would take precedence when both are used at the same time. In that case I suggest the token in the GET request wins.

In any case, my change would not alter the default behavior nor break anything.

What do you think?

---

<div class="post-metadata">

### Author: ![foxmask](https://avatars.discourse-cdn.com/v4/letter/f/c68b51/32.png) [@foxmask](https://discourse.joplinapp.org/u/foxmask)
#### Post date: [29 June 2019 06:53 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/2 "2019-06-29T06:53:46Z")

</div>

on my side I don’t have a fixed idea, but the topic has been discussed here, and a RFC says HEADER

> <https://stackoverflow.com/questions/5517281/place-api-key-in-headers-or-url>

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [29 June 2019 10:14 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/3 "2019-06-29T10:14:09Z")

</div>

I’d be fine with it but then I’d like to deprecate the GET method, as I prefer not to keep accumulating multiple methods to do auth.

That means we’ll need to update the documentation to use the header method instead, and eventually remove the GET query parameter. We can put a warning on the doc that the GET query parameter is deprecated and will be removed in 6 months for example. After 6 month, using the GET query parameter will print an error with a link to the doc.

Let’s use something standard for the header as described there: [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization)

For example:

```
Authorization: Token 123456781234567812345678123456781234567812345678

```

I see there’s a list of authentication type: [https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml](https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml) But I couldn’t find a simple summary of it so not sure if any of these would make sense for us.

---

<div class="post-metadata">

### Author: ![sciurius](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/sciurius/32/132_2.png) [@sciurius](https://discourse.joplinapp.org/u/sciurius)
#### Post date: [29 June 2019 21:42 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/4 "2019-06-29T21:42:57Z")

</div>

Seconded.

---

<div class="post-metadata">

### Author: ![tessus](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/tessus/32/13_2.png) [@tessus](https://discourse.joplinapp.org/u/tessus)
#### Post date: [29 June 2019 22:35 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/5 "2019-06-29T22:35:30Z")

</div>

It was certainly not my intention to deprecate the GET parameter and make it harder for people who already use the API.

Using the `Authorization` header is probably not a good idea. This header is mostly used by the web server and not the application on top. AFAIK, there's no `Authorization: Token xxxxxx` format either and I have never seen a token used in that way (except OAUTH which is a different story).  
We could write an RFC and submit it, even though I doubt it makes much sense, because most people would rather manage the tokens in the application layer than the web server.

In the APIs I wrote, I used the header `TOKEN: token-here`, but I've seen different names like `API-KEY`, `APIKEY`, `APITOKEN`, `API-TOKEN`, ...

This topic is rather complex, because tokens are not supposed to be used as a security measure alone, but used in combination with an authorization scheme. Some even use a token instead of the password for basic authentication. But I think we can forego an endless discussion about the principles of token utilization. (It does not apply to us anyway.)

We don't use authorization at all, so we only have to decide on a HEADER name.

The reason why I use `TOKEN` is the following: it is succinct and self explanatory. e.g. what else would be the header TOKEN for when you connect to an URL `https://example.com/api/v1/endpoint`? Developers who use an API know that they need a token, so there's no reason to use an API prefix for TOKEN. However, I've heard the argument that there could be more than just one token, in which case the prefix were to help with distinguishing them. Valid point, but we don't, thus no need. 😉

---

<div class="post-metadata">

### Author: ![sciurius](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/sciurius/32/132_2.png) [@sciurius](https://discourse.joplinapp.org/u/sciurius)
#### Post date: [30 June 2019 20:01 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/6 "2019-06-30T20:01:29Z")

</div>

I know (and use) several rest API’s that use the Authorization header. It is meant for this.  
E.g. Spotify: [https://developer.spotify.com/documentation/general/guides/authorization-guide/](https://developer.spotify.com/documentation/general/guides/authorization-guide/) (see steps 2 and 3).

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [30 June 2019 20:56 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/7 "2019-06-30T20:56:21Z")

</div>

I don’t think there’s a case to support multiple auth methods. If a header is the correct way to do that as seems to suggest foxmask and sciurius link then maybe we should switch to this. But having two methods doesn’t make much sense and makes maintenance more complex.

As for the exact format, I’ve also seen things like `X-SOMETHING: token-here`

---

<div class="post-metadata">

### Author: ![tessus](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/tessus/32/13_2.png) [@tessus](https://discourse.joplinapp.org/u/tessus)
#### Post date: [1 July 2019 03:23 UTC](https://discourse.joplinapp.org/t/api-token-as-header/2815/8 "2019-07-01T03:23:50Z")

</div>

> [@sciurius](#):
>
> I know (and use) several rest API’s that use the Authorization header

Yes, but those APIs provide authorization from the backend. You just made my point exactly. This does not apply to Joplin. There is no authorization in Joplin. The method described is more like OAUTH + API token, therefore 2 tokens. In joplin, we have one token.
