Tencent/WeKnora · error

API key is required for Google provider

Error message

API key is required for Google provider

What it means

NewGoogleProvider requires a non-empty APIKey in the WebSearchProviderParameters and returns this error immediately at construction time if it is missing. The provider is built purely from parameters (no environment fallback), so the caller must supply the Google Custom Search API key explicitly.

Source

Thrown at internal/infrastructure/web_search/google.go:27

	"google.golang.org/api/option"

	"github.com/Tencent/WeKnora/internal/logger"
	"github.com/Tencent/WeKnora/internal/types"
	"github.com/Tencent/WeKnora/internal/types/interfaces"
)

// GoogleProvider implements web search using Google Custom Search Engine API
type GoogleProvider struct {
	srv      *customsearch.Service
	apiKey   string
	engineID string
}

// NewGoogleProvider creates a new Google provider from parameters (no environment variables).
// The API endpoint is the official Google Custom Search endpoint — not tenant-configurable.
func NewGoogleProvider(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
	if params.APIKey == "" {
		return nil, fmt.Errorf("API key is required for Google provider")
	}
	if params.EngineID == "" {
		return nil, fmt.Errorf("engine ID is required for Google provider")
	}

	httpClient, err := NewSearchHTTPClient(30*time.Second, params.ProxyURL)
	if err != nil {
		return nil, err
	}
	clientOpts := []option.ClientOption{
		option.WithAPIKey(params.APIKey),
		option.WithHTTPClient(httpClient),
	}
	srv, err := customsearch.NewService(context.Background(), clientOpts...)
	if err != nil {
		return nil, err
	}
	return &GoogleProvider{

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Populate params.APIKey with a valid Google Cloud API key enabled for Custom Search API before calling NewGoogleProvider.
  2. Check the config mapping so the api_key field actually flows into WebSearchProviderParameters.
  3. Add a startup validation that fails early when provider config is incomplete.
  4. Fall back to a different configured search provider when Google parameters are absent.

Example fix

// before
provider, err := NewGoogleProvider(types.WebSearchProviderParameters{EngineID: engineID})
// after
if cfg.GoogleAPIKey == "" {
    return fmt.Errorf("google search unavailable: API key not configured")
}
provider, err := NewGoogleProvider(types.WebSearchProviderParameters{APIKey: cfg.GoogleAPIKey, EngineID: engineID})
Defensive patterns

Strategy: validation

Validate before calling

if params.APIKey == "" {
    return errors.New("google search provider requires APIKey — check config")
}
provider, err := NewGoogleProvider(params)

Try / catch

provider, err := NewGoogleProvider(params)
if err != nil {
    return nil, fmt.Errorf("init google search provider: %w", err)
}

Prevention

When it happens

Trigger: Calling NewGoogleProvider with types.WebSearchProviderParameters where params.APIKey == "".

Common situations: Config loader skipped the API key field; YAML/JSON config key typo (e.g. api_key not mapped); tenant config omits credentials; empty env var copied into params without a check.

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/73f249496adb1903. Report an issue: GitHub.