Tencent/WeKnora · error

API key is required for Metaso provider

Error message

API key is required for Metaso provider

What it means

Returned by ValidateMetasoParameters (and therefore by NewMetasoProvider) when the Metaso provider is constructed without a non-empty APIKey. Metaso's API is authenticated exclusively via a Bearer token, so the provider refuses to initialize rather than failing later at request time with a 401. This is a fail-fast configuration validation error.

Source

Thrown at internal/infrastructure/web_search/metaso.go:56

}

func NewMetasoProvider(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
	if err := ValidateMetasoParameters(params); err != nil {
		return nil, err
	}
	client, err := NewSearchHTTPClient(defaultMetasoTimeout, params.ProxyURL)
	if err != nil {
		return nil, err
	}
	return &MetasoProvider{
		client: client, baseURL: defaultMetasoSearchURL,
		apiKey: strings.TrimSpace(params.APIKey), scope: metasoScope(params.ExtraConfig),
	}, nil
}

func ValidateMetasoParameters(params types.WebSearchProviderParameters) error {
	if strings.TrimSpace(params.APIKey) == "" {
		return fmt.Errorf("API key is required for Metaso provider")
	}
	scope := metasoScope(params.ExtraConfig)
	if _, ok := validMetasoScopes[scope]; !ok {
		return fmt.Errorf("invalid Metaso search scope: %s", scope)
	}
	return nil
}

func metasoScope(extraConfig map[string]string) string {
	if scope := strings.TrimSpace(extraConfig["scope"]); scope != "" {
		return scope
	}
	return defaultMetasoScope
}

func (p *MetasoProvider) Name() string { return "metaso" }

func (p *MetasoProvider) Search(ctx context.Context, query string, maxResults int, includeDate bool) ([]*types.WebSearchResult, error) {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Set the Metaso API key in the provider parameters (APIKey field) before calling NewMetasoProvider.
  2. Verify the environment variable / secret holding the key is actually set and exported where the app runs.
  3. Check that secret injection (CI variables, .env file, secret manager) resolved to a non-empty value.
  4. Trim and check the value yourself before constructing the provider to fail with a clearer app-level message.

Example fix

// before
provider, err := NewMetasoProvider(types.WebSearchProviderParameters{})

// after
apiKey := os.Getenv("METASO_API_KEY")
if strings.TrimSpace(apiKey) == "" {
    return nil, fmt.Errorf("METASO_API_KEY not configured")
}
provider, err := NewMetasoProvider(types.WebSearchProviderParameters{APIKey: apiKey})
Defensive patterns

Strategy: validation

Validate before calling

func requireAPIKey(params types.WebSearchProviderParameters) error {
    if strings.TrimSpace(params.APIKey) == "" {
        return fmt.Errorf("API key is required for Metaso provider")
    }
    return nil
}

// call before NewMetasoProvider:
if err := requireAPIKey(params); err != nil { return err }

Try / catch

provider, err := NewMetasoProvider(params)
if err != nil {
    if strings.Contains(err.Error(), "API key is required") {
        return nil, fmt.Errorf("configuration error: set METASO_API_KEY (%w)", err)
    }
    return nil, err
}

Prevention

When it happens

Trigger: NewMetasoProvider calls ValidateMetasoParameters; strings.TrimSpace(params.APIKey) == "" — i.e. WebSearchProviderParameters.APIKey unset, empty string, or whitespace only.

Common situations: METASO_API_KEY (or equivalent env var) not set in the environment; config file missing the api_key field; secret injected as empty string because of a failed secret-manager lookup; trailing whitespace-only key after sanitization.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/cc8bf35079ade528. Report an issue: GitHub.