Skip to content
Advertisement

Discord: Get User by Id

I’m trying to create a web application to manage the users of my Discord server. In my database, I have stored only the users’ ids.

I tried to use the discord.js API, but from what I’ve understood it requires a discord bot to do that. That’s not what I want. I would like to retrieve the user’s information from my frontend, even by calling a backend function, but without having a discord bot which is always online. In other words, I need something simpler.

I would like to request users’ information by using only the id. Which is the best way to do that in JavaScript?

Advertisement

Answer

You can use the Discord API.

First, create a Discord application here. Once you’ve done that, click ‘Bot’ on the sidebar and create a bot for that application. There, you’ll see a section called ‘Token’ under the bot username. Copy this and store it somewhere secure. It is important to never share your token. If you do, you should regenerate it to prevent abuse.

You can then use the Get User endpoint (/users/{user.id}/) to retrieve the user for an ID. This should be done by the backend as it involves authenticating with the bot token.


Using the API directly

Here is a minimal example of how you would fetch a user by their ID using the Discord API using Node.js:

JavaScript

The response would be something like this:

JavaScript

Using a library

Alternatively, you may be able to use a Discord library to do this instead. The following examples also handle rate limits.

@discordjs/rest + discord-api-types

JavaScript

The result would be the same JSON as described above.

For TypeScript users:

JavaScript

discord.js

When I first posted this answer, @discordjs/rest didn’t exist yet.

JavaScript

The result of fetchUser would be a discord.js User object.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement