# Cannot add tags via the API

**URL:** <https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120>\
**Category:** Product Support\
**Created:** [May 12, 2016, 2:10pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120 "2016-05-12T14:10:59Z")\
**Posts on this page:** 7\
**Page:** 1

## AI-generated summary

**Cannot add tags via the API**

Several users are experiencing difficulties adding tags to topics via the API. [fefrei](/t/-/44120/1) initially encountered a `403 Forbidden` error, resolving the issue by including the required slug in the API call. 

[fefrei](/t/-/44120/2) shared the solution, mentioning that the slug must be correct for the API call to work. 

[Diana_Naranjo](/t/-/44120/3) then reported a similar issue, despite using the slug, and was advised by [fefrei](/t/-/44120/4) to use `Content-Type: application/x-www-form-urlencoded` instead of JSON-encoding the body. [Diana_Naranjo](/t/-/44120/5) acknowledged the mistake and corrected it.

Additionally, [huynhthai824](/t/-/44120/6) shared an error when updating tags for topics with a keyword search, but the issue is unrelated to the API tag-adding problem.

<div class="post-metadata">

**Author:** ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)\
**Post date:** [May 12, 2016, 2:10pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/1 "2016-05-12T14:10:59Z")

</div>

I’m trying to add tags via the API, but I’m doing something wrong.

Here’s a good request made from the website:

```plaintext
PUT https://example.com/t/ueber-die-kategorie-organisation/74 HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 53
Accept: */*
Origin: https://example.com
X-CSRF-Token: ■■■■■■■■■■■■■■■■■■■■■■
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: https://example.com/t/ueber-die-kategorie-organisation/74
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,de;q=0.6
Cookie: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

tags%5B%5D=archiv-organisation&tags%5B%5D=archiv-2015

```

Here’s what I am trying to do:

```plaintext
PUT https://example.com/t/74?api_key=■■■■■■■■■■■■■■■■■■■■&api_username=system HTTP/1.1
host: example.com
content-type: application/x-www-form-urlencoded
content-length: 22
Connection: close

tags%5B%5D=archiv-2015

```

The server responds with `403 Forbidden`.

Does anyone see what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)\
**Post date:** [May 12, 2016, 2:24pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/2 "2016-05-12T14:24:10Z")

</div>

Aha, I found it. As it turns out, the slug is required and must be correct for the API call to work.  
That’s slightly annoying, but a solvable problem 🙂

---

<div class="post-metadata">

**Author:** ![Diana\_Naranjo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/diana_naranjo/32/121727_2.png) [@Diana\_Naranjo](https://meta.discourse.org/u/Diana_Naranjo)\
**Post date:** [June 6, 2016, 3:29pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/3 "2016-06-06T15:29:33Z")

</div>

> [@fefrei](#):
>
> tags%5B%5D

I am trying to update tags, buteven using the slug does not procude any change  
URL: `http://forumURL/t/topic_slug/topic_id?api_key=api_key&api_username=user_name`  
PUT DATA:  
`{"title": "Some title", "tags[]":"tag_id", "category_id": "category_id"}`

---

<div class="post-metadata">

**Author:** ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)\
**Post date:** [June 7, 2016, 8:37am UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/4 "2016-06-07T08:37:58Z")

</div>

It looks like you are JSON-Encoding the body, which won’t work. Your body should look like this:

```
tags%5B%5D=archiv-2015

```

---

<div class="post-metadata">

**Author:** ![Diana\_Naranjo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/diana_naranjo/32/121727_2.png) [@Diana\_Naranjo](https://meta.discourse.org/u/Diana_Naranjo)\
**Post date:** [June 8, 2016, 6:06pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/5 "2016-06-08T18:06:55Z")

</div>

Thank you, I realize now that I was trying to set the tags using a header  
`Content-type: application/json` when it should be `Content-Type: application/x-www-form-urlencoded`

---

<div class="post-metadata">

**Author:** ![thaidb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thaidb/32/68488_2.png) [@thaidb](https://meta.discourse.org/u/thaidb)\
**Post date:** [July 6, 2019, 11:51pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/6 "2019-07-06T23:51:25Z")

</div>

Hello, i have mistake when update tag for topics with keyword search.

> Topic.find\_by(name: ‘quận 1 Dt:’).update\_all( tag: quận-1)

My console wrong? thank!

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/cannot-add-tags-via-the-api/44120/7 "2024-06-08T12:37:08Z")

</div>

Closed on June 8, 2024, 12:37pm UTC

This topic was automatically closed after 2948 days. New replies are no longer allowed.
