Published on

Sentry API: Get a List of All Users in Your Organization

Authors

At work, I came across a use case where I need to list down all the accounts a particular user (search by email) has access to.

Here is how we can do it in Sentry:

Dependency

We need to configure SENTRY_AUTH_TOKEN to interact with the API. We can create it easily using the Sentry dashboard.

Since an older version of node.js does not support fetch out of the box, we'll use node-fetch package

npm install node-fetch

Snippet

import fetch from 'node-fetch';
const YOUR_SENTRY_ORG_ID = 'example-org';
const SENTRY_API_TOKEN = '...'; // process.env.SENTRY_API_TOKEN

async function checkSentryUserExists(email) {
    try {
        const response = await fetch(`https://sentry.io/api/0/organizations/${YOUR_SENTRY_ORG_ID}/users/`, {
            headers: {
                'Authorization': `Bearer ${SENTRY_API_TOKEN}`
            }
        });

        const users = await response.json();

        for (let user of users) {
            if (user.email === email) {
                return user; // User exists
            }
        }

        return null; // User does not exist

    } catch (error) {
        console.error(error);
    }
}

checkSentryUserExists('ashik@example.com')
    .then(result => {
        if (result?.id) {
            console.log(`✅ Sentry:  account exists`);
        } else {
            console.log(`❌ Sentry does not account exists`);
        }
    });

Happy finding users!