crowdsecurity/crowdsec · error

loading api client: %w

Error message

loading api client: %w

What it means

LoadCrowdsec calls LoadAPIClient to validate/prepare the local API client section of the config. Any failure inside (bad LAPI URL, missing credentials, unreadable config) is wrapped as 'loading api client: %w'.

Source

Thrown at pkg/csconfig/crowdsec_service.go:159

	if err = c.LoadSimulation(); err != nil {
		return fmt.Errorf("load error (simulation): %w", err)
	}

	if c.Crowdsec.ParserRoutinesCount <= 0 {
		c.Crowdsec.ParserRoutinesCount = 1
	}

	if c.Crowdsec.BucketsRoutinesCount <= 0 {
		c.Crowdsec.BucketsRoutinesCount = 1
	}

	if c.Crowdsec.OutputRoutinesCount <= 0 {
		c.Crowdsec.OutputRoutinesCount = 1
	}

	if err = c.LoadAPIClient(); err != nil {
		return fmt.Errorf("loading api client: %w", err)
	}

	return nil
}

func (c *CrowdsecServiceCfg) DumpContextConfigFile() error {
	// XXX: MakeDirs
	out, err := yaml.Marshal(c.ContextToSend)
	if err != nil {
		return fmt.Errorf("while serializing ConsoleConfig (for %s): %w", c.ConsoleContextPath, err)
	}

	if err = os.MkdirAll(filepath.Dir(c.ConsoleContextPath), 0o700); err != nil {
		return fmt.Errorf("while creating directories for %s: %w", c.ConsoleContextPath, err)
	}

	if err := os.WriteFile(c.ConsoleContextPath, out, 0o600); err != nil {
		return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleContextPath, err)

View on GitHub (pinned to 909b515798)

Solutions

  1. Verify api.client.lapi_url and credentials file path in your crowdsec config
  2. Re-run `cscli lapi register` to regenerate local_api_credentials.yaml
  3. Validate the api section YAML syntax and restart
Defensive patterns

Strategy: try-catch

Validate before calling

var creds map[string]string; if b, err := os.ReadFile(lapiCredsPath); err == nil { if err := yaml.Unmarshal(b, &creds); err != nil { log.Fatalf("LAPI credentials file invalid: %v", err) } }

Try / catch

if err := cfg.LoadCrowdsec(); err != nil { if strings.Contains(err.Error(), "loading api client") { log.Fatalf("API client config problem: %v", err) } }

Prevention

When it happens

Trigger: api.client section present but invalid: malformed lapi_url, missing/incorrect credentials file (lapi credentials yaml), or internal LoadAPIClient error during LoadCrowdsec.

Common situations: Users register with `cscli lapi register` then move/lose local_api_credentials.yaml; wrong api_url port; YAML indentation mistakes in the api section.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/42303366f00934ae. Report an issue: GitHub.