To access your Sanity content programmatically, you need your Project ID and an API token. Here's how to get both.
Step 1: Find Your Project ID
Your Project ID is in your Sanity Studio URL or the Sanity dashboard:
- Go to sanity.io/manage
- Select your project
- Your Project ID is in the URL:
sanity.io/manage/project/YOUR_PROJECT_ID - It's also displayed on the project dashboard
The Project ID is a short alphanumeric string like abc123xy.
Step 2: Create an API Token
- In your project dashboard, click API in the left sidebar
- Scroll to the Tokens section
- Click Add API token
- Enter a name (e.g., "My Integration")
- Choose permissions:
- Viewer: Read-only access
- Editor: Read and write access
- Deploy Studio: For deploying Sanity Studio
- Click Save
Important: Copy the token immediately. You won't be able to see it again.
Understanding Dataset
Sanity projects can have multiple datasets (like production and development). The default is usually production. You can find your datasets:
- In the project dashboard, click Datasets in the left sidebar
- You'll see all available datasets
What Can You Do with the Sanity API?
- Query content: Fetch documents using GROQ queries
- Create documents: Add new content programmatically
- Update content: Modify existing documents
- Manage assets: Upload and organize images and files
Troubleshooting
"Invalid token" error
- Make sure you copied the complete token
- Verify the token hasn't been revoked
- Check you're using the correct Project ID
"Insufficient permissions" error
- Verify your token has Editor permissions (not just Viewer) if you're writing data
- Check the token has access to the dataset you're querying
"Dataset not found" error
- Verify the dataset name (usually
production) - Check the dataset exists in your project settings
Automate Your Content Workflows
Once you have your API credentials, you can connect Sanity to Miniloop to automate content creation, publishing, and management using natural language. Describe what content you need, and Miniloop handles the rest.
Frequently Asked Questions
What's the difference between Viewer and Editor tokens?
Viewer tokens can only read content. Editor tokens can read, create, update, and delete content. Choose Editor if you need to modify content programmatically.
Can I use one token for multiple datasets?
Yes, a single token can access all datasets in your project, based on its permission level.
How do I revoke a token?
Go to your project dashboard > API > Tokens and delete the token. This immediately revokes access.
What's GROQ?
GROQ is Sanity's query language for fetching content. It's similar to GraphQL but specific to Sanity. You use GROQ queries to retrieve documents from your datasets.