How to Fix Notion API Token Invalid or Unauthorized Errors
In this tutorial, you'll learn about How to Fix Notion API Token Invalid or Unauthorized Errors. We cover key concepts, practical examples, and best practices.
The Notion API uses integration tokens to authenticate requests. When your API call returns 401 Unauthorized or 400 Bad Request: Invalid token, the token was revoked, the integration was not shared with the database, or the token lacks the required capabilities.
The Problem
POST /v1/pages
Authorization: Bearer secret_abc123
Returns:
{
"object": "error",
"status": 401,
"code": "unauthorized",
"message": "API token is invalid."
}
Wrong approach — hardcoding a different token in the source code.
The Fix
Verify the token is active:
1. Go to https://www.notion.so/my-integrations
2. Find your integration
3. If the token was revoked, click "Show" and copy the new token
4. Update your code with the new token
Ensure the integration has access to the database:
1. Open the Notion database
2. Click "Share" (top-right)
3. In "Invite," search for your integration name
4. Click "Invite" — the integration now has access
Check the capabilities granted:
1. In the integration settings, check "Capabilities"
2. Read content, Update content, Insert content, etc.
3. If your API call requires "Insert content" but only "Read content" is granted, it will fail
4. Update capabilities and re-share the integration with the database
Expected output:
API returns 200 with the requested data
Pages can be created, read, updated, and searched
No 401 or 403 errors for any endpoint
Prevention Tips
- Store API tokens in environment variables, not in code
- Rotate tokens periodically for security
- Grant the minimum required capabilities to each integration
- After changing capabilities, re-share the integration with all databases it needs
- Test API calls with a tool like Postman before implementing in code
Common Mistakes with api token
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world NOTION code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Related: DodaTech's API Token Manager securely stores Notion integration tokens, monitors usage, and rotates tokens automatically on a configurable schedule. Use with DodaZIP for encrypted token backup.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro