Skip to content

Instantly share code, notes, and snippets.

@Lissy93
Created February 27, 2024 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lissy93/2568a009cf1c9b7ecb801307ebb4ac7b to your computer and use it in GitHub Desktop.
Save Lissy93/2568a009cf1c9b7ecb801307ebb4ac7b to your computer and use it in GitHub Desktop.
A quick and easy Cloudflare Worker, for fetching all the GitHub Sponsors of a given user
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const { pathname } = new URL(request.url);
const username = pathname.split('/')[1];
if (!username) {
return new Response(
'Simple API to fetch GitHub Sponsors of a given user 💖\nUsage: GET /[username]',
{ status: 200 }
);
}
const query = `
query {
user(login: "${username}") {
sponsorshipsAsMaintainer(first: 100) {
edges {
node {
sponsorEntity {
... on User {
login
name
avatarUrl
}
... on Organization {
name
avatarUrl
}
}
}
}
}
}
}
`;
try {
const apiResponse = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${GH_API_TOKEN}`,
'User-Agent': 'GetUserSponsors'
},
body: JSON.stringify({ query })
});
const result = await apiResponse.json();
if (!apiResponse.ok) {
throw new Error(result.errors ? result.errors.map(e => e.message).join('; ') : 'Failed to fetch GitHub data');
}
// Extracting sponsor details
const sponsors = result.data.user.sponsorshipsAsMaintainer.edges.map(edge => edge.node.sponsorEntity);
return new Response(JSON.stringify(sponsors), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response(error.message, { status: 500 });
}
}
@Lissy93
Copy link
Author

Lissy93 commented Feb 27, 2024

About
This is a very simple API, in the form of a Cloudflare Worker, which lets you request a list of all sponsors for a given GitHub user.
There's no CORS, so should work from anywhere, no auth, and just a single easy HTTP GET request.

Why? Because currently, GitHub does not provide a REST endpoint for fetching sponsors. Instead you must use their GraphQL API, which will not work for frontend applications.

For example, I'm using this here to thank the sponsors of my project (using this code).


Usage
Just send a GET request to http://github-sponsors-api.as93.net/[username]

The response will be an array of users, with each object containing login (GitHub username), name (user's full name) and avatarUrl (the path to their profile picture). For example:

[
  { "login": "octocat", "name": " The Octocat", "avatarUrl": "https://avatars.githubusercontent.com/u/583231" }
]

Example: http://github-sponsors-api.as93.net/lissy93


Deployment

  1. Setup Node.js, if you haven't already done so, then install wrangler, npm i -g wrangler
  2. Create a new worker project, with wrangler init sponsors-api
  3. Navigate into your project, and install deps with cd sponsors-api and npm install
  4. In your wrangler.toml add your API key to the GITHUB_TOKEN env var
  5. Start the development server, and test everything's working with wrangler dev
  6. Finally, run wrangler deploy to deploy your app to the world!

Alternatively, just log in to Cloudflare, navigate to "Workers" and then "New" / "Quick Edit".
And then paste the above script in, and then hit "Save & Deploy". Done :)

You can now make requests to https://[project-name].workers.dev/[user] 🚀


License
Licensed under MIT, © Alicia Sykes 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment