Skip to content

Get a Github user

For example, we can get a user without passing an authentication token.

GitHub API endpoints have a cURL command that you can manually translate into a fetchJSON call (e.g. see Get a user).

GitHub API endpoints have a cURL command that you can manually translate into a fetchJSON call (e.g. see Get a user).

Here’s a Val that returns a GitHub user object for a given username:

@vtdocs/getGithubUserRun in Val Town ↗
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const getGithubUser = async (username: string) => {
const user = await fetchJSON(
`https://api.github.com/users/${username}`,
);
return user;
};

You can alter it, or just call it directly like this:

Usage of @vtdocs/getGithubUserRun in Val Town ↗
import { getGithubUser } from "https://esm.town/v/vtdocs/getGithubUser";
console.log(await getGithubUser("stevekrouse"));

You can also do this with octokit.js. Reminder: import npm packages by prepending the package name with npm:.

Usage of npm:@octokit/coreRun in Val Town ↗
import { Octokit } from "npm:@octokit/core";
export const getGithubUserViaOctokit = async (username: string) => {
const octokit = new Octokit();
const user = await octokit.request("GET /users/{username}", {
username,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});
return user;
};
Fork it on