Table of Contents
Intro
For some time I've been wanting to be able to get my current GitHub Sponsors data using an API. The API GitHub offers is currently set for GraphQL only, so it was a bit of a struggle to get the data, because I'm not really much familiar with GraphQL.
Besides that, setting up GraphQL in a project where only one query would be used, seemed like overkill for me. Anyway, I found out that we could send the GraphQL query in a normal REST API request using the body parameter. I used this repository for reference.
TL:DR; You can find the finalized project code at https://github.com/jahirfiquitiva/sponsors-edge-api
The GraphQL Query
It took me quite a while to explore the GitHub GraphQL API docs and build the query to get all the info needed so I'll skip the details. I basically used their GraphQL Explorer and went through multiple Interface
s, Object
s and other data, while doing a trial-and-error process to get the final query.
Some of the data I used was:
- User
- Sponsorable
- SponsorConnection
- SponsorsListing
- SponsorsTierConnection
- SponsorsTier
- SponsorsTierAdminInfo
- Sponsorship
- Sponsor
And the final query is as follows:
What does this query do?
Basically, it gets the GitHub Sponsors listing data and the sponsors total count for the user defined at the query beginning: user(login: "jahirfiquitiva")
here I used my GitHub username, but you can replace it with yours.
I am getting the listing data because I wanted to group my sponsors by their tier, as well as knowing the tier price and other details. If you only wanted to know the sponsors, regardless of their tier, a simpler query might be built using the
sponsors
property at the end of the query above.
From the Sponsors listing data, I get the different tiers. A tier is basically a donation option, for example, I have 6 monthly tiers: star, crystal ball, rocket, robot, lightning and diamond based on different price. This is because I “named” them although they don't really have a name by default. This just helps me categorize my sponsors.
tiers(first: 20)
will return the first 20 tiers from your sponsors listing. As I said, I have 6 monthly tiers, and 3 one-time tiers: 9 tiers in total, so even 20 is more than needed. Also, you can only have a total of 10 published monthly tiers and 10 published one-time tiers.
From each tier, I get the following info:
monthlyPriceInDollars
: How much this tier costs per month in USD.isOneTime
: Whether this tier is only for use with one-time sponsorships.isCustomAmount
: Whether this tier was chosen at checkout time by the sponsor rather than defined ahead of time by the maintainer who manages the Sponsors listing.name
: The name of the tier. (iirc this name is just something like${price} per month
, which might not be very helpful depending on the use case)description
: The description of the tier. (in MarkDown format)adminInfo
: Object that contains thesponsorships
property, which includes:totalRecurringMonthlyPriceInDollars
: The total amount in USD of all recurring sponsorships in the connection whose amount you can view. Does not include one-time sponsorships.nodes
: Which would correspond to data related to the sponsorship, including the sponsor information using thesponsorEntity
property.sponsorEntity
can be a User or Organization, so that's why we access both.- Additionally,
sponsorEntity
includestierSelectedAt
which identifies the date and time when the given tier was chosen for this sponsorship.
- Additionally,
sponsorships(first: 100)
returns the first 100 nodes for this sponsorship tier. I don't really have many sponsors, so this one is fine for me. If you have more sponsors, you'll have to look into pagination for this property.
If you need more information about your sponsors or the sponsorships, you can explore the docs.
Authorization
Before we actually use this query to access this data, we must create a Personal Access Token, as it requires authorization.
To do so, go to your GitHub account Settings, then go to Developer Settings and finally select Personal Access Tokens, or just follow this link: https://github.com/settings/tokens
There, click on the Generate new token
button, give it a specific name, set the expiration period to one you'd like, although this one only reads data, so I think No expiration
is fine.
The scopes required for this query are:
- admin:org
- read:org
- user
- read:user
Then scroll down and click on Generate token
Make sure to save the token in a safe and accessible place, as you won't be able to access its value ever again.
Project setup
Let's create a new NextJS project. We'll use TypeScript in this guide, so we do it with the following command:
Replace {folder}
with the name of the folder you want the project to be at.
Now, open the project with your favorite editor or IDE.
Create a .env.local
file with the Personal Access Token previously generated:
You can name the variable differently, but be careful when we access it in code later.
Initial request
Let's quickly setup the API and the code to make an initial request.
Aiming to keep things organized, let's create a folder named lib
on the project root, and another folder named sponsors
inside.
Create a file named request.ts
:
Put the query content from the one showed at the beginning of this post, inside the backticks in
graphQlQuery
Here we are getting GitHub Personal Access Token (PAT) via the GH_PAT
environment variable (setup previously in .env.local
), and creating a function that will do a simple fetch POST request to https://api.github.com/graphql
sending the PAT in an Authorization
header, then get the JSON body from the response and return it.
Now create a file named index.ts
:
Here we just export everything already exported in the request.ts
file.
Now, let's setup the API route. Go to file pages/api/hello.ts
and rename it to sponsors.ts
, there, modify it to look like this:
Here, we import the function previously created, then we call it using the async
function handler
, then we get the JSON body from it and return it as our API response.
Testing
Let's test our simple API, in order to do that, run npm run dev
or yarn dev
in your Terminal or CMD from the project root.
Once the project is running, you'll see this:
Next, go to http://localhost:3000/api/sponsors
, if everything was setup correctly, you will see the raw response from the API, which format isn't really nice to read or use, and looks like this:
I recommend using the JSON viewer extension, to read the response more easily
Typing raw response
Let's define interfaces for the raw response from the GitHub GraphQL API. Create a file named types.ts
under the lib/sponsors
folder.
We can start with the deepest nested object, which would be the sponsorEntity
Now we can go one level up to sponsorships
Since adminInfo
only includes the sponsorships
property, let's go a couple levels up at a time:
Now let's go up to sponsorsListing
And finally the whole response:
Now, can import the SponsorsResponse
interface in request.ts
and type the getSponsorsGraphQLResponse
function, so it will look like:
Typing the response won't affect anything in the API as it is at this point, but will allow us to transform that data into a more readable format in an easy way.
Transforming the raw response
First, let's plan the desired object format to make the response easier to read:
With this format, we have an object with 2 properties: tiers
and total
. Tiers will have all its corresponding information including a sponsors
object array with the information for each sponsor. There's a couple fields renamed from the raw response, to make them a bit simpler:
login
→username
avatarUrl
→avatar
websiteUrl
→website
tierSelectedAt
→since
totalRecurringMonthlyPriceInDollars
→totalEarningsPerMonth
totalCount
→total
Let's create the interfaces for this new object in lib/sponsors/types.ts
:
Finally, let's create a function to transform the SponsorsResponse
object into the Sponsors
one, in the lib/sponsors/request.ts
file:
Finally, let's update our API to use the new function:
Now if we go to http://localhost:3000/api/sponsors
, we will see a response with the format we initially planned for. 🎉
You can try the deployed version of this endpoint at https://sponsors-edge-api.vercel.app/api/sponsors
Extra: Edge Runtime
Additionally, and this is completely optional, we can modify the API to use the new Edge Runtime.
The Next.js Edge Runtime is based on standard Web APIs. The Edge API routes, enable you to build high performance APIs with Next.js. Using the Edge Runtime, they are often faster than Node.js-based API Routes.
To achieve this, we can modify the API to be like:
As you can see, we change the type NextApiRequest
for NextRequest
, remove the res
parameter from the handler function, and change the way we return the JSON response by using the Response
object.
You can try the deployed version of this endpoint at https://sponsors-edge-api.vercel.app/api/sponsors-edge
Closing up
That's it, now you have an API to get your GitHub sponsors and use that data to anything you'd like.
I have used it to list my sponsors in my donate page, for example. I hope it's useful for you too.
You can find the finalized project code at https://github.com/jahirfiquitiva/sponsors-edge-api.
Have a great day! 👋😀