SubtitleEdit/subtitleedit · error · Exception

API key invalid (or perhaps billing is not enabled)?

Error message

API key invalid (or perhaps billing is not enabled)?

What it means

Thrown by GoogleTranslateV2 (official Google Cloud Translation API v2 with an API key) when the POST returns HTTP 400 Bad Request. Google returns 400 when the API key is malformed, missing, or when the key is valid but the Cloud Translation API is not enabled / billing is not active on the project.

Source

Thrown at src/libuilogic/AutoTranslate/GoogleTranslateV2.cs:76

                var result = await _httpClient.PostAsync(uri, new StringContent(string.Empty), cancellationToken);

                if (!result.IsSuccessStatusCode)
                {
                    try
                    {
                        Error = await result.Content.ReadAsStringAsync(cancellationToken);
                        SeLogger.Error($"Error in {StaticName}.Translate: " + Error);
                    }
                    catch
                    {

                        // ignore
                    }
                }

                if ((int)result.StatusCode == 400)
                {
                    throw new Exception("API key invalid (or perhaps billing is not enabled)?");
                }
                if ((int)result.StatusCode == 403)
                {
                    throw new Exception("\"Perhaps billing is not enabled (or API key is invalid)?\"");
                }

                if (!result.IsSuccessStatusCode)
                {
                    throw new Exception($"An error occurred calling GT translate - status code: {result.StatusCode}");
                }

                content = await result.Content.ReadAsStringAsync(cancellationToken);
            }
            catch (WebException webException)
            {
                var message = string.Empty;
                if (webException.Message.Contains("(400) Bad Request"))
                {

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Verify GoogleApiV2Key is set and non-empty in Settings.
  2. In the GCP console, enable the Cloud Translation API for the key's project.
  3. Confirm billing is enabled on the GCP project (Translation requires billing even for the free tier).
  4. Check the API key restrictions allow the Translation API and the calling IP.

Example fix

// before
if ((int)result.StatusCode == 400)
{
    throw new Exception("API key invalid (or perhaps billing is not enabled)?");
}

// after - read Google's JSON error to report the exact reason instead of guessing
if ((int)result.StatusCode == 400)
{
    throw new Exception($"GoogleTranslateV2 returned 400: {Error}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate the V2 key is present and looks like a Google API key before calling
var key = Configuration.Settings.Tools.GoogleApiV2Key;
if (string.IsNullOrWhiteSpace(key))
    throw new InvalidOperationException("Google Translate V2 API key is not set.");
if (key.Length < 30)
    throw new InvalidOperationException("Google Translate V2 API key looks too short - verify it was copied fully.");

Type guard

public static bool LooksLikeGoogleV2Key(string key)
    => !string.IsNullOrWhiteSpace(key) && key.StartsWith("AIza", StringComparison.Ordinal) && key.Length >= 35;

Try / catch

try
{
    return await googleV2.Translate(text, src, tgt, token);
}
catch (Exception ex) when (ex.Message.Contains("API key invalid"))
{
    throw new InvalidOperationException("Google returned 400 - the API key is invalid or the Translation API / billing is not enabled.", ex);
}

Prevention

When it happens

Trigger: Translate() posts to https://translation.googleapis.com/language/translate/v2/?...&key=<key>; result.StatusCode is 400. Note the key is sent as a query parameter (line 54), so a malformed key lands here.

Common situations: Empty or whitespace API key (GoogleApiV2Key not set); key from a project where the Translation API is not enabled; billing disabled on the GCP project; key copied with extra characters; key restricted to an API/project that does not include Translation.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/0221785068b5cbce. Report an issue: GitHub.