How to give a user a complimentary Ghost subscription
(Assuming they already exist in Ghost)
async function giveComplimentaryAccess() {
//try {
console.log(`Granting complimentary access to member ${memberId} for tier ${tierId}...`);
// First, get the member to verify they exist
const memberToken = generateToken(ghostAdminApiKey);
const memberResult = await fetch(`${ghostApiUrl}/ghost/api/admin/members/${memberId}/?include=tiers`, {
method: 'GET',
headers: {
'Authorization': `Ghost ${memberToken}`,
'Content-Type': 'application/json',
'Accept-Version': 'v6.8'
}
});
const memberData = await memberResult.json();
const member = memberData?.members?.[0];
if (!member) {
throw new Error('Member not found');
}
// Check if the user already has an active status tier that isn't this one.
try {
const activeTiers = member?.tiers?.filter(tier => tier.id !== tierId) ||
member?.subscriptions?.filter(subscription => subscription.tier.id !== tierId && subscription.status === 'active');
if (activeTiers?.length > 0) {
throw new Error('User already has an active status tier that isn\'t this one');
}
let tierSetResult = await fetch(`${ghostApiUrl}/ghost/api/admin/members/${memberId}/?include=tiers`, {
method: 'PUT',
headers: {
'Authorization': `Ghost ${memberToken}`,
'Content-Type': 'application/json',
'Accept-Version': 'v6.8'
},
body: JSON.stringify({
members: [{
id: memberId,
tiers: [{id: tierId, expiry_at: '2026-01-01'}]
}]
})
});
const tierSetData = await tierSetResult.json();
return true;
} catch (error) {
console.error('Error:', error);
return false;
}
}