
TypeScript function to get a GitHub user's email from their public event commit history via the GitHub API.
This function first looks for the user's email in recent commits (last 30 days), and if not there, looks at commits in repos they own.
import { emailsFromGitHubUsername } from "https://esm.town/v/stevekrouse/github-user-email/index.ts";
// Recommended, combines both functions, only uses the more expensive one if the cheaper one fails
const emails = await emailsFromGitHubUsername("stevekrouse");
console.log(emails); // [ "steveykrouse@gmail.com" ]
If you want to go in the reverse direction (from an email to a github username), check out sindresorhus/github-username.
import githubUsername from "npm:github-username";
console.log(await githubUsername("sindresorhus@gmail.com"));
//=> 'sindresorhus'
This function pulls the user's recent public events.
import { emailsFromGitHubEvents } from "https://esm.town/v/stevekrouse/github-user-email/events.ts";
const emails = await emailsFromGitHubEvents("stevekrouse");
console.log(emails); // [ "steveykrouse@gmail.com" ]
The limitation is that this endpoint only gets events from the last 30 days, so if the user hasn't committed in the last month, you won't get any results from this method.
Pulls commits from repos that the username owns. Uses up your GitHub rate limit much faster.
import { emailsFromGitHubCommits } from "https://esm.town/v/stevekrouse/github-user-email/commits.ts";
const commitsEmails = await emailsFromGitHubCommits("stevekrouse");
console.log(commitsEmails); // [ "steveykrouse@gmail.com" ]
The GitHub API
rate limits
unauthenticated requests to 60 per hour per IP address. As a quick workaround,
Val Town provides an IP-randomizer fetch
proxy that makes it easy to get
around these limits. These functions use that proxy by default. However, if
you're using these functions at scale, please use a GITHUB_TOKEN to be a good
GitHub citizen.
- Go to https://github.com/settings/personal-access-tokens/new
- Give it a name like
Get user emails
- Set the expiration appropriately (I recommend
No expiration
) - Click Generate token
- Copy the token into your val's env variables (in the left sidebar) as
GITHUB_TOKEN