# How to grant badges using Automation?

**URL:** https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716
**Category:** Support
**Tags:** automation
**Created:** [July 13, 2022, 8:45am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716 "2022-07-13T08:45:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Helga\_Razinkova](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/helga_razinkova/32/249626_2.png) [@Helga\_Razinkova](https://meta.discourse.org/u/Helga_Razinkova)
#### Post date: [July 13, 2022, 8:45am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/1 "2022-07-13T08:45:19Z")

</div>

sorry, me again 🙂

You mentioned that Automation plugin can help with granting badges. Maybe you have some hints on how to do so? I cannot find anything relatable in the dropdown:

 ![image](https://global.discourse-cdn.com/meta/original/4X/9/5/5/9550a332cb89309f3144716c495b048dda921490.png)

---

<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: [July 13, 2022, 9:16am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/2 "2022-07-13T09:16:08Z")

</div>

Looking at the [commit](https://meta.discourse.org/t/add-grant-badge-trigger/232715/3), it seems it should be available for `send_pms` and `zapier_webhook` scripts:

> This trigger needs a `badge` as required field and accepts an optional `only_on_first_grant` field. ATM it’s usable for: `send_pms` and `zapier_webhook` scripts.

 ![image](https://global.discourse-cdn.com/meta/original/4X/8/1/8/818caccdd4caa1af119becdc5094b6a9569b5fe8.png)

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [July 13, 2022, 9:20am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/3 "2022-07-13T09:20:03Z")

</div>

Sorry it might not be super clear, so let me explain it.

ATM we only have a trigger for granted badge, which means we can calls scripts when a user is awarded a badge. But we don’t exactly have what you currently ask for `a script to grant a badge` which would be triggered on some event (joining a specific group? ). As explained, our best solution so far is [Creating triggered custom badge queries](https://meta.discourse.org/t/triggered-custom-badge-queries/19336)

---

<div class="post-metadata">

### Author: ![Helga\_Razinkova](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/helga_razinkova/32/249626_2.png) [@Helga\_Razinkova](https://meta.discourse.org/u/Helga_Razinkova)
#### Post date: [July 13, 2022, 11:43am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/15 "2022-07-13T11:43:00Z")

</div>

Thank you so much for your help!

I really hope that we’ll manage to set an API to grant a badge every time a user posts their first post 🙏🏻

Thank you!

---

<div class="post-metadata">

### Author: ![Gynn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gynn/32/467937_2.png) [@Gynn](https://meta.discourse.org/u/Gynn)
#### Post date: [July 22, 2022, 5:47am UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/16 "2022-07-22T05:47:56Z")

</div>

This is very helpful!

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [August 25, 2022, 12:53pm UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/17 "2022-08-25T12:53:36Z")

</div>

Hey Helga,

If you want to automatically grant a badge, the best way to do this is via the SQL Query for the badge.  
This can be found In admin-\>badges

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/4/a/d4a5e3ed65d0f5222c2b9203049958b4f8dca94d.png)

Then scroll down to find the SQL query.

 ![image](https://global.discourse-cdn.com/meta/original/4X/8/2/8/8289bb970d5ef985518da8dd40932f7510e07359.png)

If you search through [meta.discourse.org](http://meta.discourse.org) you might be lucky enough to find someone has already written the query you are looking for.

My favourite query grants a badge when someone joins a group. The example below grants the badge within 24 hours of the person joining the “discourse-admin” group

```sql
select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = (
  select g.id from groups g where g.name = 'discourse-admin'
)

```

I use the following settings with the query for the badge

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/b/d/bbd29d4b88e85e1b8a4e58948a276611a5ba0b29.png)

---

<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: [August 25, 2022, 1:04pm UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/18 "2022-08-25T13:04:38Z")

</div>

For that you would need to enable custom SQL badges, which isn’t available to everyone depending on their hosting:

> [@Enable Badge SQL](https://meta.discourse.org/t/enable-badge-sql/47894/1):
>
> If you are an [Enterprise customer](https://discourse.org/buy), contact [@team](https://meta.discourse.org/groups/team) via PM to enable it. (You may also contact team by emailing `team@discourse.org`).
> 
> Instead of SQL queries, our [Standard and Business](https://discourse.org/buy) customers can see [how to grant a custom badge through the API](https://meta.discourse.org/t/how-to-grant-a-custom-badge-through-the-api/103270).

Though using the API is also suggested as an alternative.

---

<div class="post-metadata">

### Author: ![Tris20](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tris20/32/264639_2.png) [@Tris20](https://meta.discourse.org/u/Tris20)
#### Post date: [August 25, 2022, 1:08pm UTC](https://meta.discourse.org/t/how-to-grant-badges-using-automation/232716/19 "2022-08-25T13:08:03Z")

</div>

Oh wow, I didn’t know about that. Thanks for the tip @JammyDodger!

I guess it would also be possible to get the result of an SQL query from the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin, then use the api to grant the badge.
