{"record":{"id":"6fe7b2b1b65a5d44","repo":"charmbracelet/crush","slug":"failed-to-set-provider-api-key-status-code-d","errorCode":null,"errorMessage":"failed to set provider API key: status code %d","messagePattern":"failed to set provider API key: status code (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":122,"sourceCode":"\t\t\treturn fmt.Errorf(\"failed to marshal oauth token: %w\", err)\n\t\t}\n\t\traw = b\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported api key type %T\", apiKey)\n\t}\n\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/provider-key\", id), nil, jsonBody(proto.ConfigProviderKeyRequest{\n\t\tScope:      scope,\n\t\tProviderID: providerID,\n\t\tKind:       kind,\n\t\tAPIKey:     raw,\n\t}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to set provider API key: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to set provider API key: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// ImportCopilot attempts to import a GitHub Copilot token on the\n// server.\nfunc (c *Client) ImportCopilot(ctx context.Context, id string) (*oauth.Token, bool, error) {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/import-copilot\", id), nil, nil, nil)\n\tif err != nil {\n\t\treturn nil, false, fmt.Errorf(\"failed to import copilot: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, false, fmt.Errorf(\"failed to import copilot: status code %d\", rsp.StatusCode)\n\t}\n\tvar result struct {\n\t\tToken   *oauth.Token `json:\"token\"`\n\t\tSuccess bool         `json:\"success\"`","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/client/config.go#L104-L140","documentation":"SetProviderAPIKey expects HTTP 200 from the provider-key endpoint. Any other status (404 workspace missing, 401/403 auth failure, 500 server error) is reported with this error carrying the numeric status.","triggerScenarios":"Server responds non-200 to POST /workspaces/{id}/config/provider-key — nonexistent workspace id, invalid/expired auth, or server-side persistence error.","commonSituations":"Typo in workspace id, stale auth token after re-login elsewhere, server version lacking the provider-key endpoint, key rejected by validation on the server.","solutions":["Read the status code in the error to identify the failure class","For 401/403, refresh credentials and retry","For 404, verify the workspace id exists","For 5xx, check server logs and retry after the server recovers"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := c.SetProviderAPIKey(ctx, wsID, scope, pid, key); err != nil {\n    var sc int\n    if n, _ := fmt.Sscanf(err.Error(), \"failed to set provider API key: status code %d\", &sc); n == 1 {\n        switch {\n        case sc == 401 || sc == 403:\n            return reauthAndRetry(ctx)\n        case sc == 404:\n            return errors.New(\"workspace not found\")\n        }\n    }\n    return err\n}","preventionTips":["Verify workspace id validity before mutating config","Keep server and client versions aligned","Authenticate before sequences of config calls"],"tags":["http","api-key","client"],"backgroundTag":"http-non-200-response","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}