Last active
February 28, 2024 15:15
-
-
Save ggomaeng/7895a2e410874ca87f5534890055d20c to your computer and use it in GitHub Desktop.
Reliable way to retrieve Farcaster user data using fid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ky from 'ky'; | |
import { getUserDataForFid } from 'frames.js'; | |
export const HUBS = [ | |
{ | |
shortname: 'nemes', | |
url: 'nemes.farcaster.xyz:2281', | |
ssl: true, | |
fid: 9152, | |
version: '1.10.0', | |
write: false, | |
}, | |
{ | |
shortname: 'hoyt', | |
url: 'hoyt.farcaster.xyz:2281', | |
ssl: true, | |
fid: 9152, | |
version: '1.10.0', | |
write: false, | |
}, | |
{ | |
shortname: 'lamia', | |
url: 'lamia.farcaster.xyz:2281', | |
ssl: true, | |
fid: 9152, | |
version: '1.10.0', | |
write: false, | |
}, | |
{ | |
shortname: 'standard_crypto', | |
url: 'hub.farcaster.standardcrypto.vc:2281', | |
ssl: true, | |
fid: 69, | |
version: '1.10.0', | |
contact: 'https://github.com/standard-crypto/farcaster-hub-gcp', | |
}, | |
{ | |
shortname: 'pinata', | |
url: 'hub.pinata.cloud', | |
ssl: true, | |
fid: 20918, | |
version: '1.10.0', | |
write: true, | |
contact: 'https://www.pinata.cloud/farcaster', | |
}, | |
{ | |
shortname: 'freefarcasterhub', | |
url: 'hub.freefarcasterhub.com:3281', | |
ssl: true, | |
fid: 1356, | |
version: '1.10.0', | |
write: true, | |
contact: 'https://freefarcasterhub.com', | |
}, | |
{ | |
shortname: 'noderpc', | |
url: 'www.noderpc.xyz/farcaster-mainnet-hub', | |
ssl: true, | |
fid: 191696, | |
version: '1.10.0', | |
write: true, | |
contact: 'https://www.noderpc.xyz', | |
}, | |
{ | |
shortname: 'farcasthub', | |
url: 'api.farcasthub.com:2281', | |
ssl: false, | |
fid: 20140, | |
version: '1.10.0', | |
write: true, | |
contact: 'https://farcasthub.com', | |
}, | |
] as const; | |
// Available Farcaster HUB Urls https://foss.farchiver.xyz | |
export async function getAvailableHub() { | |
try { | |
// ping all hubs and return the first one that responds | |
const promises = HUBS.map((hub) => ky.get(`https://${hub.url}/v1/info`)); | |
const firstResp = await Promise.any(promises); | |
const first = await firstResp.json<(typeof HUBS)[number]>(); | |
return first?.url; | |
} catch { | |
return undefined; | |
} | |
} | |
export async function getUserDataWithFid(fid: number): Promise<{ | |
username?: string; | |
avatarUrl?: string; | |
}> { | |
const user = await getUserDataForFid({ | |
fid, | |
options: { | |
hubHttpUrl: await getAvailableHub(), | |
}, | |
}); | |
if (!user) throw new Error('User not found'); | |
const { username, profileImage } = user; | |
return { | |
username, | |
avatarUrl: profileImage, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment