Grant a custom badge through the API

If you can’t enable badge SQL, it would be technically possible to automate this, but it would be a two step process. First, make an API request to run a Data Explorer query that returns the usernames of users who meet your criteria who have not yet been granted the badge: Run Data Explorer queries with the Discourse API. Then, use the usernames returned by that query to grant the badge via the API.

This feels kind of inefficient though. As far as I can tell, there isn’t a route that can be used to grant a badge to multiple users with a single API call, so depending on how many users qualify for the badge, you could end up making a lot of API requests.

Ultimately, this feels like the kind of thing that could be handled by the Automation plugin. There could be a recurring trigger that runs a Data Explorer query with a given query ID. Based on the fields returned by the query, the results could be used to trigger various scripts. For example, if a username field was returned by the query, it could be used to run scripts that awarded badges, added users to groups, etc.

1 Like