# Discourse API Gem - how to create topic with tags?

**URL:** https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869
**Category:** Support
**Created:** [July 6, 2020, 3:31am UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869 "2020-07-06T03:31:54Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 3:31am UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/1 "2020-07-06T03:31:54Z")

</div>

This isn’t working…

```plaintext
client.create_topic(
  title: "My Title",
  category: "13",
  raw: "My Post",
  tags: "testtag"
)

```

Since we require at last one tag, I get:

```plaintext
DiscourseApi::UnprocessableEntity ({"action"=>"create_post", "errors"=>["You must select at least 1 tag."]}):

```

Any idea how we create topics with tags via the Discourse gem?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 7:15am UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/2 "2020-07-06T07:15:26Z")

</div>

Maybe make it `tags:["testtag"]`

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 3:11pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/3 "2020-07-06T15:11:40Z")

</div>

That doesn’t work either Faizaan (same message that a tag is required).

Not sure if this is out of date, but it seems that tags are different to the others being some sort of array type - but how do we use that via the gem?

> [@Can't create topic with tag using admin API due to insufficient trust level](https://meta.discourse.org/t/cant-create-topic-with-tag-using-admin-api-due-to-insufficient-trust-level/46972/4):
>
> This worked for me and added two tags: curl --data "api\_key=asdf...asdf&api\_username=neill&title=this%20one%20has%20two%20tags&raw=and%20it%20is%20twice%20as%20delicious&tags[]=mazda&tags[]=protege" http://localhost:3000/posts.json [image] So I’m not sure why it isn’t working for you…

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 3:14pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/4 "2020-07-06T15:14:40Z")

</div>

If you look at the network tab while making a successful request, then you can translate it to how to do it with the gem.

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 3:17pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/5 "2020-07-06T15:17:40Z")

</div>

It needs this format:

> [@How find tags id to add tags when create topic via API](https://meta.discourse.org/t/how-find-tags-id-to-add-tags-when-create-topic-via-api/84387/4):
>
> You should try like `tags[]="testtag"` . It’s an array type.

But how to do that with the gem?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 3:34pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/6 "2020-07-06T15:34:21Z")

</div>

This is what I illustrated in my above post.

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 3:44pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/7 "2020-07-06T15:44:16Z")

</div>

…but it does not work:

> DiscourseApi::UnprocessableEntity ({“action”=\>“create\_post”, “errors”=\>[“You must select at least 1 tag.”]}):

😕

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 3:44pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/8 "2020-07-06T15:44:55Z")

</div>

Can you try some other tags?

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 3:47pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/9 "2020-07-06T15:47:19Z")

</div>

Haha that’s what I was thinking too so just did - the last few I tried definitely exist (but still doesn’t work).

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 3:57pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/10 "2020-07-06T15:57:53Z")

</div>

Can you turn off required tags on a category to see whether the thing actually works?

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 4:37pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/11 "2020-07-06T16:37:59Z")

</div>

Do you mean the gem? It works as I use it to make posts in existing topics (from the same script in fact), send PMs etc. This is the first time I am trying to add tags though..

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [July 6, 2020, 4:40pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/12 "2020-07-06T16:40:18Z")

</div>

Ok, I see. I’m sure you are doing but just confirming

```plaintext
client.create_topic(
  title: "My Title",
  category: "13",
  raw: "My Post",
  tags: ["testtag", "testtag2"]
)

```

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [July 6, 2020, 4:42pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/13 "2020-07-06T16:42:54Z")

</div>

Looking at the source code for the discourse\_api gem it doesn’t look like it supports creating topics with tags yet. I’ll add it to my list.

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 6, 2020, 7:17pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/14 "2020-07-06T19:17:14Z")

</div>

Thanks Blake 🙂

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [July 9, 2020, 6:53pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/15 "2020-07-09T18:53:39Z")

</div>

Okay, you can create Topics with tags now using the latest version (0.42.0) of the discourse\_api gem.

You just need to pass the tags in an array like so:

> [@fzngagan](#):
>
> ```plaintext
> client.create_topic(
> title: "My Title",
> category: "13",
> raw: "My Post",
> tags: ["testtag", "testtag2"]
> )
> 
> ```

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [July 9, 2020, 10:03pm UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/16 "2020-07-09T22:03:09Z")

</div>

Wow - that was fast Blake!

I wasn’t expecting an update so soon and so managed to find a workaround by using the post method and passing everything as params. Doing it with create\_topic is definitely better tho, thanks!

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [July 11, 2020, 12:00am UTC](https://meta.discourse.org/t/discourse-api-gem-how-to-create-topic-with-tags/156869/17 "2020-07-11T00:00:08Z")

</div>

This topic was automatically closed after 29 hours. New replies are no longer allowed.
