Created
December 27, 2024 12:13
-
-
Save IMB11/b215dfc92d827c03d05f4981308cfd12 to your computer and use it in GitHub Desktop.
This script changes your bluesky handle. For custom domains make sure you have everything setup like the bluesky frontend tells you (DNS record or .well-known) file.
This file contains hidden or 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
// Run using node handle.js | |
// Install axios using npm install axios | |
// nodejs v18+ | |
const axios = require('axios'); | |
// ===================== | |
// === Configuration === | |
// ===================== | |
// Replace these constants with your actual credentials and desired handle | |
const PDS_HOST = 'https://bsky.social'; // Your PDS host (include https://) | |
const BLUESKY_HANDLE = 'XXXXXX.bsky.social'; // Your current Bluesky handle (without @) | |
const APP_PASSWORD = 'XXXXXX'; // A Bluesky app password | |
const NEW_HANDLE = 'EXAMPLE.COM'; // The new handle you want to set | |
// =========================== | |
// === Function Definitions === | |
// =========================== | |
/** | |
* Creates a session and obtains a JWT. | |
* @returns {Promise<string>} The access JWT. | |
*/ | |
async function createSession() { | |
const apiUrl = `${PDS_HOST}/xrpc/com.atproto.server.createSession`; | |
const payload = { | |
identifier: BLUESKY_HANDLE, | |
password: APP_PASSWORD | |
}; | |
try { | |
const response = await axios.post(apiUrl, payload, { | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}); | |
const { accessJwt } = response.data; | |
if (!accessJwt) { | |
throw new Error('accessJwt not found in response.'); | |
} | |
console.log('Authentication successful.'); | |
return accessJwt; | |
} catch (error) { | |
if (error.response) { | |
console.error('Error creating session:', error.response.data); | |
} else { | |
console.error('Error creating session:', error.message); | |
} | |
process.exit(1); | |
} | |
} | |
/** | |
* Updates the handle using the provided JWT. | |
* @param {string} jwt - The access JWT obtained from createSession. | |
*/ | |
async function updateHandle(jwt) { | |
const apiUrl = `${PDS_HOST}/xrpc/com.atproto.identity.updateHandle`; | |
const payload = { | |
handle: NEW_HANDLE | |
}; | |
try { | |
const response = await axios.post(apiUrl, payload, { | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${jwt}` | |
} | |
}); | |
console.log('Handle updated successfully:', response.data); | |
} catch (error) { | |
if (error.response) { | |
console.error('Error updating handle:', error.response.data); | |
} else { | |
console.error('Error updating handle:', error.message); | |
} | |
process.exit(1); | |
} | |
} | |
/** | |
* Main function to execute the update process. | |
*/ | |
async function main() { | |
console.log('Authenticating...'); | |
const jwt = await createSession(); | |
console.log(`Updating handle to "${NEW_HANDLE}"...`); | |
await updateHandle(jwt); | |
console.log('Handle update process completed.'); | |
} | |
// ================== | |
// === Script Start === | |
// ================== | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment