Create Flairs with snoowrap

Understand how to create flairs using the JavaScript Reddit API Wrapper.

Create flairs

Flair is used as a tag for a post or to tag a Reddit user, which allows other users to browse that particular post or a user in the community with the help of that flair. The flair can be created by the moderator or by the user if the moderator allows its users. To create a flair for a user, we'll use the assignFlair() method of the getUser method. We’ll also use the the text parameters because we didn't have any available flair that we could use.

Once a flair gets created, it will be reflected on the subreddit with the flair username. If anyone wants to tag the user, it can be tagged with its flair.

Request parameters

Here are some important parameters we'll use to call the method:

Parameter

Type

Category

Description

text

string

optional

This is the title of the flair.

cssClass

string

optional

This is the CSS class that is used by the user's flair.

Click "Run" to add a new flair.

Press + to interact
import snoowrap from "snoowrap";
const reddit = new snoowrap({
client_id: "{{CLIENT_ID}}",
client_secret: "{{CLIENT_SECRET}}",
refresh_token: "{{REFRESH_TOKEN}}",
username: "{{USERNAME}}",
user_agent: "testscript by u/{{USERNAME}}",
});
async function createFlair() {
try {
const response = await reddit
.getUser("{{USERNAME}}")
.assignFlair({
subredditName: "{{SUBREDDIT_NAME}}",
text: "This is a sample flair",
text_editable: "False",
});
printResponse(response);
} catch (e) {
console.log(e);
}
}
createFlair();

Let's understand the code given above:

  • Lines 3–9: We initialize the snoowrap requester. The parameters are explained in the Parameters for Making a Snoowrap Requester lesson in the Appendix

  • Line 10: We defined an async function with the name of createFlair in which we passed all the required parameters to create the flair.

Response fields

This request returns a JSON response, which contains the username associated with the flair.