Awarding a Discourse badge via the api

In the last few posts I’ve been building a little ruby app for automatically checking if I went to the gym or not. So in this post we are going to see how I can award myself a badge on my private discourse forum via the API, but maybe now that I have this blog I should add it here instead :thinking:.

I’ll be following this guide about how to do this.

The first thing I did was setup a custom badge on my site and checked the box that says it can awarded multiple times. Now that the badge has been created, you can get the id for it by going to /badges and clicking on your new badge. You will then see the id in the url.

And here is the cURL command you can use to assign a badge to a user:

curl -X POST https://blog.blake.app/user_badges \
-H "Api-Username: api" \
-H "Api-Key: api-key" \
-F "badge_id=104" \
-F "username=blake"

The next step is to do this with ruby and the discourse_api gem, but I guess I’ll have to save that for another day.