{"record":{"id":"87a9e22f60d3a9d4","repo":"we-promise/sure","slug":"api-key-is-required","errorCode":null,"errorMessage":"api_key is required","messagePattern":"api_key is required","errorType":"validation","errorClass":"Provider::Trading212::ConfigurationError","httpStatus":null,"severity":"error","filePath":"app/models/provider/trading212.rb","lineNumber":40,"sourceCode":"  MAX_PAGES = 200\n  PAGE_LIMIT = 50\n\n  RETRYABLE_ERRORS = [\n    SocketError,\n    Net::OpenTimeout,\n    Net::ReadTimeout,\n    Errno::ECONNRESET,\n    Errno::ECONNREFUSED,\n    Errno::ETIMEDOUT,\n    EOFError\n  ].freeze\n\n  default_options.merge!({ timeout: 60 }.merge(httparty_ssl_options))\n\n  attr_reader :api_key, :api_secret, :environment\n\n  def initialize(api_key:, api_secret:, environment: \"live\")\n    raise ConfigurationError, \"api_key is required\" if api_key.blank?\n    raise ConfigurationError, \"api_secret is required\" if api_secret.blank?\n    raise ConfigurationError, \"Invalid environment: #{environment}\" unless %w[live demo].include?(environment.to_s)\n\n    @api_key = api_key.to_s.strip\n    @api_secret = api_secret.to_s.strip\n    @environment = environment.to_s\n  end\n\n  def fetch_account_summary\n    get(\"/equity/account/summary\")\n  end\n\n  def fetch_positions\n    get(\"/equity/positions\")\n  end\n\n  def fetch_instruments\n    get(\"/equity/metadata/instruments\")","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/provider/trading212.rb#L22-L58","documentation":"Provider::Trading212::ConfigurationError raised in the constructor when the required api_key keyword argument is blank (nil or empty/whitespace string). Trading 212's API uses Basic auth built from api_key:api_secret, so the client refuses to instantiate without a key. This fails fast before any HTTP request is made.","triggerScenarios":"Calling Provider::Trading212.new(api_key: nil, api_secret: \"secret\"), passing api_key: \"\" or \"   \" (whitespace survives .blank? check), or reading the key from an ENV var / settings record that was never set (ENV[\"TRADING212_API_KEY\"] returns nil).","commonSituations":"Fresh environment where TRADING212_API_KEY is not in .env.local; a user connects a Trading 212 account but never entered an API key in the settings UI; a YAML/credentials file renamed the key; test suite constructing the provider without stubbing the key.","solutions":["Set the API key before constructing: export TRADING212_API_KEY=... (or add it to .env.local) and pass it through","Verify the value is present and non-empty in the code path that builds the provider: raise a user-facing validation error if credentials are missing instead of letting the provider raise","If the key comes from a settings/account record, check record.api_key.present? before instantiating Provider::Trading212","Confirm you copied the full key from Trading 212's Settings > API section (keys are long; partial pastes are blank after strip)"],"exampleFix":"// before\nclient = Provider::Trading212.new(\n  api_key: ENV[\"TRADING212_API_KEY\"],\n  api_secret: ENV[\"TRADING212_API_SECRET\"]\n)\n\n// after\napi_key = ENV[\"TRADING212_API_KEY\"]\napi_secret = ENV[\"TRADING212_API_SECRET\"]\nraise ArgumentError, \"Set TRADING212_API_KEY and TRADING212_API_SECRET\" if api_key.blank? || api_secret.blank?\n\nclient = Provider::Trading212.new(api_key: api_key, api_secret: api_secret)","handlingStrategy":"validation","validationCode":"api_key = ENV[\"TRADING212_API_KEY\"]\nraise ArgumentError, \"TRADING212_API_KEY is blank\" if api_key.blank?\n# or, from a settings record:\nraise \"Connect Trading 212 and enter an API key first\" if account_provider.api_key.blank?","typeGuard":null,"tryCatchPattern":"begin\n  client = Provider::Trading212.new(api_key:, api_secret:, environment: \"live\")\nrescue Provider::Trading212::ConfigurationError => e\n  # config problem, not transient — show setup UI, never retry\nend","preventionTips":["Validate credentials at the settings/connection layer before any sync job constructs the provider","Include placeholder keys in .env.local.example so setup fails visibly","Fail CI early: assert required ENV vars are present at boot in production"],"tags":["ruby","trading212","configuration","missing-api-key","constructor"],"backgroundTag":"missing-api-key","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}