Requests
WARNING
In API v2, all attributes must be explicitly requested.
You can use the methods on the client to access the API. If no token is given in the client options, you can use the token
option in the request options.
ts
import { type PatreonClient, QueryBuilder } from 'patreon-api.ts'
export async function fetchPatreon (client: PatreonClient) {
// Get the full name of the creator of the campaign
const query = QueryBuilder.campaigns
.addRelationships(['creator'])
.addRelationshipAttributes('creator', ['full_name'])
// get the campaigns
const campaigns = await client.fetchCampaigns(query)
// or fetch the post(s), member(s), post(s) or current user
// Or list resources to paginate multiple pages
for await (const campaigns of client.paginateCampaigns(query)) {
for (const campaign of campaigns.data) {
const creatorId = campaign.relationships.creator.data?.id
const creator = campaigns.included.find(t => t.id === creatorId)
console.log(`Campaign ${campaign.id} is created by ${creator?.attributes.full_name}`)
}
}
}
ts
import { type PatreonClient, QueryBuilder, Routes, Type, type PatreonQuery } from 'patreon-api.ts'
export async function fetchPatreonRaw (client: PatreonClient) {
type Query = PatreonQuery<Type.Campaign, 'creator', {
user: ('full_name')[]
}, true>
const query = QueryBuilder.fromParams<Query>(new URLSearchParams({
include: 'creator',
'fields[user]': 'full_name',
}))
// get the campaigns
const campaigns = await client.fetchOauth2(Routes.campaigns(), query)
// or fetch the post(s), member(s), post(s) or current user
// Or list resources to paginate multiple pages
for await (const campaigns of client.paginateOauth2(Routes.campaigns(), query)) {
for (const campaign of campaigns.data) {
console.log(campaign.id)
}
}
}
The methods on the client return the default JSON:API
response. Use the simplified
or normalized
properties to access the simplified methods.
Query builder
Include all
To fetch all attributes and relationships on every request, you can use the rest.includeAllQueries
client options.
Routes
Fetch a campaign
ts
import { PatreonCreatorClient, QueryBuilder } from 'patreon-api.ts'
declare const client: PatreonCreatorClient
const campaignQuery = QueryBuilder.campaign
.addRelationships(['tiers'])
.setRelationshipAttributes('tiers', ['amount_cents', 'title'])
const campaign = await client.fetchCampaign('campaign_id', campaignQuery)
const tiers = campaign.included.map(item => item.attributes)
Fetch campaigns
ts
import { PatreonCreatorClient, QueryBuilder } from 'patreon-api.ts'
declare const client: PatreonCreatorClient
const campaignsQuery = QueryBuilder.campaigns
.setAttributes({ campaign: ['patron_count'] })
const campaigns = await client.fetchCampaigns(campaignsQuery)
for (const campaign of campaigns.data) {
console.log(campaign.id, campaign.attributes.patron_count)
}
ts
import { PatreonCreatorClient, QueryBuilder } from 'patreon-api.ts'
declare const client: PatreonCreatorClient
const campaignsQuery = QueryBuilder.campaigns
.setAttributes({ campaign: ['patron_count'] })
for await (const page of client.paginateCampaigns(campaignsQuery)) {
for (const campaign of page.data) {
console.log(campaign.id, campaign.attributes.patron_count)
}
}