{"record":{"id":"d26d5cf28855a61d","repo":"we-promise/sure","slug":"unauthorized-d26d5c","errorCode":"unauthorized","errorMessage":"Invalid API key","messagePattern":"Invalid API key","errorType":"exception","errorClass":"Provider::Redbark::AuthenticationError","httpStatus":401,"severity":"critical","filePath":"app/models/provider/redbark.rb","lineNumber":240,"sourceCode":"    def auth_headers\n      {\n        \"Authorization\" => \"Bearer #{@api_key}\",\n        \"Content-Type\" => \"application/json\",\n        \"Accept\" => \"application/json\"\n      }\n    end\n\n    # Redbark error envelope: { error: { message, code, details } }\n    # Error messages carry the parsed provider message only, never the raw\n    # response body - callers log and re-log these strings.\n    def handle_response(response)\n      case response.code\n      when 200, 201\n        JSON.parse(response.body, symbolize_names: true)\n      when 400\n        raise Error.new(\"Bad request: #{error_message_from(response)}\", :bad_request)\n      when 401\n        raise AuthenticationError.new(\"Invalid API key\", :unauthorized)\n      when 403\n        raise AuthenticationError.new(\"Access forbidden - your Redbark plan may not include API access\", :access_forbidden)\n      when 404\n        raise Error.new(\"Resource not found\", :not_found)\n      when 410\n        raise Error.new(\"Endpoint requires an accountId: #{error_message_from(response)}\", :bad_request)\n      when 429\n        raise RateLimitError.new(\"Rate limit exceeded\", :rate_limited)\n      when 500..599\n        raise ServerError.new(\"Redbark server error (#{response.code})\", :server_error)\n      else\n        raise Error.new(\"Unexpected response #{response.code}: #{error_message_from(response)}\", :unknown)\n      end\n    end\n\n    def error_message_from(response)\n      parsed = JSON.parse(response.body)\n      parsed.dig(\"error\", \"message\") || \"no error message provided\"","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/provider/redbark.rb#L222-L258","documentation":"Raised by Provider::Redbark's handle_response on HTTP 401: the Bearer API key is invalid. Raised as AuthenticationError (a subclass of Provider::Redbark::Error) with error_type :unauthorized. Note a blank key never gets this far — validate_configuration! in the constructor raises ConfigurationError first, so 401 means a key was present but rejected.","triggerScenarios":"Any Redbark request (list_accounts, list_connections, get_balances, get_transactions) sent with a wrong, revoked, or rotated-out API key in the Authorization: Bearer header.","commonSituations":"Key from a different environment (staging key in prod), key rotated on the Redbark dashboard but not updated in settings, typo/whitespace when pasting the key, key revoked for account inactivity.","solutions":["Re-copy the API key from the Redbark dashboard and update it in the provider settings","Confirm the key belongs to the same environment/account the data expects","Strip whitespace/newlines when storing the key","If the key was rotated, update every environment that caches it"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"def valid_redbark_key?(api_key)\n  api_key.is_a?(String) && api_key.strip.match?(/\\A\\S{8,}\\z/)\nend\n\nraise ArgumentError, \"Redbark API key looks invalid\" unless valid_redbark_key?(key)","typeGuard":"def redbark_auth_error?(error)\n  error.is_a?(Provider::Redbark::AuthenticationError) && error.error_type == :unauthorized\nend","tryCatchPattern":"begin\n  redbark.list_connections\nrescue Provider::Redbark::AuthenticationError => e\n  raise unless e.error_type == :unauthorized\n  disable_provider_connection!(provider: \"redbark\", reason: \"invalid_api_key\")\n  notify_user_to_reenter_key\nend","preventionTips":["Strip whitespace when persisting the API key","Keep per-environment keys in credentials/ENV and audit them on deploy","Disable sync jobs immediately on 401 instead of retrying — auth errors never self-heal"],"tags":["redbark","http-401","authentication","api-key"],"backgroundTag":"invalid-api-key","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}