Issue with setting up client to automatically upload wallet address, resolved with correction of dataset.
It sounds like you're trying to write data from the browser but hitting a permissions issue. Even though you're not seeing errors, this is often a sign that the write operation is silently failing due to authentication or CORS issues. Here's what's likely happening and how to fix it:
The Core Issue: Write Tokens in Frontend Code
Based on what you're describing, you probably have a Sanity client configured in your frontend code that's trying to create documents with wallet addresses. The most common issue is that write tokens should never be used directly in client-side code for security reasons.
When you say you can add documents from schema.js, that's likely happening in a server-side context (like during development or through the Studio), which is why it works. But browser-based writes need a different approach.
The Right Solution: Server-Side Endpoint
Instead of writing directly from the browser, you should:
- Create a server-side API endpoint (using Next.js API routes, serverless functions, or similar)
- Send the wallet address from your frontend to this endpoint
- Use a write token on the server to create the Sanity document
Here's a quick example with Next.js:
// pages/api/save-wallet.js (server-side)
import { createClient } from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
token: process.env.SANITY_WRITE_TOKEN, // Store this in env variables
apiVersion: '2024-01-01',
useCdn: false
})
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' })
}
const { walletAddress } = req.body
try {
const result = await client.create({
_type: 'userWallet', // or whatever your schema type is
walletAddress: walletAddress,
timestamp: new Date().toISOString()
})
res.status(200).json({ success: true, id: result._id })
} catch (error) {
res.status(500).json({ error: error.message })
}
}Then from your frontend:
// Frontend code
async function saveWallet(walletAddress) {
const response = await fetch('/api/save-wallet', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ walletAddress })
})
const data = await response.json()
console.log(data) // Check for errors here
}Alternative: Sanity Functions
If you're looking for an even more integrated solution, consider using Sanity Functions, which are serverless functions that run within Sanity's infrastructure. This eliminates the need for external hosting and provides better security out of the box.
About CORS
CORS typically isn't the issue for write operations—it's more about authentication. However, if you do need to configure CORS for your Sanity project (for read operations), you can do this in your project settings at manage.sanity.io.
Token Setup
Make sure you have a robot token with write permissions created in your Sanity Manage dashboard (under API settings), and store it as an environment variable like SANITY_WRITE_TOKEN that's only accessible server-side.
The key takeaway: never expose write tokens in frontend code. Always proxy write operations through a server-side endpoint where you can safely use your token and validate the data before writing to Sanity.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.