Integrations · Data and spreadsheets

Validate email addresses in BigQuery

Two BigQuery remote functions: validate_email(email) returns the status of each address (valid, invalid, catch_all, unknown or disposable), and validate_email_details(email) returns the reason, the suggested action, the suggested correction and more as JSON. Behind them is a small Cloud Run function in your own Google Cloud project that checks each address with our API.

SELECT email, `PROJECT_ID.DATASET_ID.validate_email`(email) AS status
FROM `PROJECT_ID.DATASET_ID.customers`;

Download email-validation-bigquery.zip: it unpacks into a folder email-validation-bigquery holding the function (function/: plain Node.js 22 or later, with only the Functions Framework that runs it as a dependency), the SQL (sql/create_functions.sql, sql/check_new_addresses.sql) and LICENSE (MIT). It has not run in a real Google Cloud project yet: it was built from Google's documentation and tested against stand-ins.

What you need

  • An API key from the API keys page with only the permission Validate addresses and a daily credit cap, used for nothing else.
  • A Google Cloud project with billing on (Cloud Run needs it), BigQuery, Cloud Run and Secret Manager, rights to create functions, connections and routines there (BigQuery Admin or the same permissions), and the gcloud and bq command-line tools. No computer of your own needed: Google's Cloud Shell (the terminal button in the Google Cloud console) runs in your browser with gcloud and bq already installed. Upload the zip there from Cloud Shell's menu, unpack it with unzip email-validation-bigquery.zip, and run the steps below in it. It is still a terminal: there is no setup by clicks alone.

What it costs

Every non-NULL row a query passes to these functions is checked each time the query runs, and a definite result costs one credit; the same address twice in one batch is checked once. unknown results, malformed addresses and test addresses on sandbox.email-validation.org are free.

  • A query over a million rows can spend a million credits, and running it again spends them again: BigQuery never caches a remote function's results, so a view, a dashboard tile or a scheduled query that calls the function checks every row on every run.
  • BigQuery may send a batch again after a timeout or a network error, even one the function answered, and that batch is charged again.
  • A query that fails part-way keeps none of its results, but the rows it already checked were charged.
  • Calling both functions on a row is two checks, and a WITH clause that calls the function and is read twice can be two checks.

So check each address once into a table (below) and join to it. Cloud Run and BigQuery bill your Google Cloud account for the function's run time and the queries.

Set it up

Placeholders: PROJECT_ID, DATASET_ID, LOCATION (the dataset's location, such as US or EU), REGION (a Cloud Run region inside it, such as us-central1 or europe-west1) and CONNECTION_ID.

  1. The key in Secret Manager, never in SQL or in the function's source:

    gcloud secrets create ev-api-key --replication-policy=automatic --data-file=-
    # paste the key, press Enter, then Ctrl-D
    gcloud secrets add-iam-policy-binding ev-api-key \
      --member=serviceAccount:RUNTIME_SERVICE_ACCOUNT --role=roles/secretmanager.secretAccessor

    The runtime service account is the Compute Engine default one unless you choose another.

  2. Deploy the function from the unpacked folder, with authentication required:

    cd email-validation-bigquery
    gcloud run deploy ev-validate-email --source=function --function=validateEmail \
      --base-image=nodejs24 --region=REGION --no-allow-unauthenticated \
      --timeout=300 --concurrency=1 --max-instances=10 \
      --set-secrets=EV_API_KEY=ev-api-key:latest

    Never allow unauthenticated calls: anyone with the URL could spend the key's credits. Add --set-env-vars=EV_REGION=eu to check addresses on our EU host. Its URL (gcloud run services describe ev-validate-email --region=REGION --format='value(status.url)') is the endpoint the SQL needs.

  3. A BigQuery connection and its service account:

    bq mk --connection --location=LOCATION --project_id=PROJECT_ID \
      --connection_type=CLOUD_RESOURCE CONNECTION_ID
    bq show --connection PROJECT_ID.LOCATION.CONNECTION_ID   # note serviceAccountId
  4. Let only that connection call the function:

    gcloud run services add-iam-policy-binding ev-validate-email --region=REGION \
      --member=serviceAccount:CONNECTION_SERVICE_ACCOUNT --role=roles/run.invoker
  5. Create the functions: run sql/create_functions.sql with your PROJECT_ID, DATASET_ID, LOCATION, CONNECTION_ID and the function's URL as ENDPOINT_URL. Its last statement is a free test on test addresses: valid, invalid, disposable, NULL.

Check each address once

sql/check_new_addresses.sql creates the table DATASET_ID.ev_email_results, then on every run picks the distinct addresses of your table that have no status yet (at most 10,000: the LIMIT caps how many addresses one run checks), calls validate_email_details(email) once per address into a temporary table, merges the results in and shows the credits it used. Replace PROJECT_ID, DATASET_ID and SOURCE_TABLE.email_column in it and run it as one script, when new addresses arrive (a scheduled query works), starting with a small LIMIT. Read the results with a join, which costs nothing:

SELECT u.*, r.status, r.reason, r.action
FROM `PROJECT_ID.DATASET_ID.SOURCE_TABLE` u
LEFT JOIN `PROJECT_ID.DATASET_ID.ev_email_results` r ON r.email = u.email_column;

Never wrap the function in IF, CASE, COALESCE or a MERGE … WHEN clause: BigQuery then sends those rows one per request instead of in batches. Filter first, then call the function on every remaining row, as the script does.

What the functions return

validate_email(email) returns one of the five statuses, or NULL for a NULL or empty address and for a row that could not be checked. validate_email_details(email) returns JSON with the same keys on every row ({status, reason, action, did_you_mean, domain, free_email, role_account, disposable, catch_all_domain, mx_found, processed_at, credits_used, request_id, error, error_detail}); read one with JSON_VALUE(details, '$.reason'). Statuses and reasons explains each value.

When a row is not checked

A row that could not be checked never gets an invented status: validate_email(email) returns NULL and the details' error says why: a rate limit (rate_limited, abuse_throttled), a short outage (service_unavailable, upstream_capacity), no credits (insufficient_credits), the key's daily cap (key_daily_cap), an address the API refused (invalid_request), no answer in time ("no_answer", which may have been charged and is never sent again), an unreadable answer (unexpected_response, the same), or "not_sent" when the batch stopped or ran out of time first.

When our API asks to slow down before anything in a batch cost a credit, the function answers BigQuery 429 and BigQuery sends the batch again by itself. Our API allows 20 validation requests a second and 100 in flight until your account has made a purchase, and 500 a second after one; lower --max-instances if batches stop on rate_limited.

A wrong key, a key without Validate addresses, a missing EV_API_KEY or a setting out of range fails the whole query with a message that names the problem, never the key.

Privacy

The function sends each address only in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org), never to another host, and follows no redirect. It logs one line per batch with counts, statuses, error codes and request ids: never an address, the key, or the BigQuery user who ran the query. What keeps addresses is under your control: BigQuery's job history (any address written into the SQL), its temporary result tables and DATASET_ID.ev_email_results.

Licence

The download is under the MIT licence (LICENSE in the zip): you may use, change and share it.

More on BigQuery

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us