charmbracelet/crush · warning
failed to marshal api key string: %w
Error message
failed to marshal api key string: %w
What it means
SetProviderAPIKey marshals a string API key to JSON before sending it. json.Marshal on a plain Go string cannot realistically fail, so this error is defensive; if it ever occurs the JSON encoding layer itself is broken (e.g. corrupted encoding state via custom marshalers).
Source
Thrown at internal/client/config.go:94
}
return nil
}
// SetProviderAPIKey sets a provider API key on the server. The wire
// format tags the credential with an explicit Kind so the server can
// decode it back into the right Go type — JSON's `any` loses that
// information across the socket.
func (c *Client) SetProviderAPIKey(ctx context.Context, id string, scope config.Scope, providerID string, apiKey any) error {
var (
kind proto.APIKeyKind
raw json.RawMessage
)
switch v := apiKey.(type) {
case string:
kind = proto.APIKeyKindString
b, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("failed to marshal api key string: %w", err)
}
raw = b
case *oauth.Token:
if v == nil {
return fmt.Errorf("oauth token is nil")
}
kind = proto.APIKeyKindOAuth
b, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("failed to marshal oauth token: %w", err)
}
raw = b
default:
return fmt.Errorf("unsupported api key type %T", apiKey)
}
rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/provider-key", id), nil, jsonBody(proto.ConfigProviderKeyRequest{
Scope: scope,View on GitHub (pinned to 7944b8e522)
Solutions
- Inspect the wrapped %w error for details from json.Marshal
- If apiKey is a custom string type, check whether it defines a MarshalJSON method that errors
- Retry the call; if it persists, report a bug in the client
Defensive patterns
Strategy: try-catch
Try / catch
if _, err := json.Marshal(apiKey); err != nil {
return fmt.Errorf("key not serializable: %w", err)
}
// proceed with SetProviderAPIKey Prevention
- Pass plain string API keys, not custom wrapper types with marshalers
- Treat this error as a bug and report it if it ever occurs
When it happens
Trigger: Calling SetProviderAPIKey with a string apiKey where json.Marshal(v) returns an error — practically never for a plain string.
Common situations: Essentially unreachable in normal use; would indicate a corrupted runtime or a custom string type with a failing MarshalJSON method.
Related errors
- failed to marshal oauth token: %w
- failed to decode workspaces: %w
- failed to decode MCP pending auth: %w
- failed to decode MCP auth URL: %w
- marshal request: %w
AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29).
Data as JSON: /api/errors/1d7bdd0e05680d43.
Report an issue: GitHub.