clockworklabs/SpacetimeDB · error · anyhow::Error
Failed to get token: {}
Error message
Failed to get token: {} What it means
After browser approval, the CLI exchanges the web session token for a SpacetimeDB token via POST api/spacetimedb-token. A body-level success=false fails the exchange; the server's error string is included when present, else 'Unknown error' (login.rs:277).
Source
Thrown at crates/cli/src/subcommands/login.rs:277
#[derive(Deserialize, Debug)]
struct SpacetimeDBTokenData {
token: String,
}
async fn spacetimedb_login(remote: &Url, web_session_token: &String) -> Result<String, anyhow::Error> {
let client = reqwest::Client::new();
let response: SpacetimeDBTokenResponse = client
.post(remote.join("api/spacetimedb-token")?)
.header("Authorization", format!("Bearer {web_session_token}"))
.send()
.await?
.error_for_status()?
.json()
.await?;
if !response.success {
return Err(anyhow::anyhow!(
"Failed to get token: {}",
response.error.unwrap_or("Unknown error".to_string())
));
}
Ok(response.data.unwrap().token.clone())
}
#[derive(Debug, Clone, Deserialize)]
struct LocalLoginResponse {
pub token: String,
}
async fn spacetimedb_direct_login(host: &Url) -> Result<String, anyhow::Error> {
let client = reqwest::Client::new();
let response: LocalLoginResponse = client
.post(host.join("/v1/identity")?)
.send()
.await?View on GitHub (pinned to 524b4487d9)
Solutions
- Restart the login flow and complete the browser approval without pausing
- Read the embedded server error string for the specific reason
- Update CLI and server to matching versions and retry against a healthy node
Defensive patterns
Strategy: retry
Try / catch
for i in 1 2; do spacetime login && break # complete browser approval immediately after it opens echo "token exchange failed, retrying" >&2; sleep 2 done
Prevention
- Finish the browser approval step without delay
- Restart the login flow on exchange failure instead of retrying the browser step
- Avoid login across server restarts or redeploys
When it happens
Trigger: Completing browser approval, then the token-exchange step failing server-side — e.g. the session token expired, was invalidated by a server restart, or is unknown to the node handling the exchange.
Common situations: Waiting too long between browser approval and exchange; server restart or load balancing to a node without the session; CLI/server version skew; clock skew affecting token validity.
Related errors
- Unknown error
- Response data is missing.
- Session token is missing in response.
- Failed to request token
- Issuer too long: {:?}
AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16).
Data as JSON: /api/errors/66a4b0e4c2f76b46.
Report an issue: GitHub.