Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created June 20, 2021 17:38
Show Gist options
  • Save steveruizok/94f660c3b18dafdc5c4ea811d852ea23 to your computer and use it in GitHub Desktop.
Save steveruizok/94f660c3b18dafdc5c4ea811d852ea23 to your computer and use it in GitHub Desktop.
Use the GitHub API to check whether a given user is sponsoring you.
/**
* A function that will return whether the given user is sponsoring you.
* Requires a personal access token from GitHub.
* See https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
*
* @param userA The sponsoring user.
*/
async function isUserSponsoringMe(username: string) {
const res = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `bearer ${process.env.YOUR_GITHUB_PERSONAL_ACCESS_TOKEN}`,
},
body: JSON.stringify({
query: `
query {
user(login: "${process.env.YOUR_GITHUB_ID}") {
isSponsoredBy(accountLogin: "${username}")
}
}
`,
}),
}).then((res) => res.json())
return res?.data?.user?.isSponsoredBy || false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment