Tokens and user IDs change unexpectedly. How can I automate refreshing them? Example:
const apiClient = new APIClient({
currentUser: process.env.CURRENT_USER,
sessionToken: process.env.SESSION_TOKEN
});
Tokens and user IDs change unexpectedly. How can I automate refreshing them? Example:
const apiClient = new APIClient({
currentUser: process.env.CURRENT_USER,
sessionToken: process.env.SESSION_TOKEN
});
In my experience, the best approach is to use a headless browser or script that directly interacts with your Notion web session instead of directly trying to auto-refresh the token from an API endpoint. The Notion token can be somewhat volatile and Notion hasn’t provided an official API for regenerating tokens, which means workarounds may break with updates. Using a solution that mimics the browser’s behavior to retrieve the latest token from cookies or session data has been reliable. Just remember to carefully handle authentication and keep security considerations in mind when automating this process.
In my view, leveraging a repeated extraction method using browser automation can be effective. I built a Node.js script that periodically accesses browser cookies to fetch the updated token directly. This method, while requiring careful handling of session data, aligns more closely to your actual usage scenario compared to crafting a custom API endpoint. Relying on official tooling for such automation is not an option, so adopting a process that simulates user behavior helps mitigate unexpected token changes, provided you incorporate regular updates in response to Notion’s evolving security measures.
hey i use a puppeter script that autologins and grabs the token from cookies. its a bit hacky and might break if notion tweaks things, but it works for now. just be extra careful with token storage cuz its not the best fix.