Official SDK

C SDK

The official C SDK is C11 with one header, an opaque client and one free function for every object it returns, plus a header-only C++17 wrapper with RAII types and exceptions. It carries its own JSON reader and its own SHA-256 / HMAC-SHA256, so the core library needs only the C standard library and POSIX; the built-in HTTP transport uses the system libcurl and is optional. It is tested with Apple clang on macOS.

C library
libemail_validation
Registry
a package manager
State
Not published yet

Not published yet. The C library libemail_validation is not on a package manager yet, so there is no install command to run today. The SDK is complete and tested and coming soon to its registry. Until then, use the code samples, which call the API directly, for example curl.

Install

The install command appears here once libemail_validation is on a package manager (coming soon).

Validate an address

#include <email_validation.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  emailval_client *client = NULL;
  emailval_error *err = NULL;
  if (emailval_client_new(getenv("EV_API_KEY"), NULL, &client, &err) != EMAILVAL_OK) {
    fprintf(stderr, "%s\n", err != NULL ? err->message : "out of memory");
    emailval_error_free(err);
    return 1;
  }
  emailval_validation_result *result = NULL;
  if (emailval_validate(client, "accepted@sandbox.email-validation.org", NULL, NULL, &result, &err) != EMAILVAL_OK) {
    fprintf(stderr, "%s\n", err != NULL ? err->message : "out of memory");
    emailval_error_free(err);
    emailval_client_free(client);
    return 1;
  }
  printf("%s %s %s\n", result->status, result->reason, result->action); /* valid accepted send */
  emailval_validation_result_free(result);
  emailval_client_free(client);
  return 0;
}

Create the client once and share it: it is immutable and safe to use from several threads. Options go in an emailval_validate_options (NULL sends none): ip_address, timeout (seconds the check may take, 5–15) and fresh. fresh = EMAILVAL_TRUE checks the address again even when it was checked in the last 12 months (see repeat checks). Nullable fields are NULL strings or values with has_value, and a status, reason or action this SDK version does not know yet is kept as its raw string. Every string in a result belongs to it until you free it.

The C++ wrapper (email_validation.hpp) returns the same results in std::unique_ptrs and throws on errors:

#include <email_validation.hpp>

#include <cstdlib>
#include <iostream>

int main() {
  namespace ev = email_validation;
  try {
    const char *key = std::getenv("EV_API_KEY");
    ev::Client client(key != nullptr ? key : "");
    ev::ValidationResult result = client.validate("accepted@sandbox.email-validation.org");
    std::cout << result->status << " " << result->reason << " " << result->action << "\n";
  } catch (const ev::Error &e) {
    std::cerr << e.what() << "\n";
    return 1;
  }
  return 0;
}

EU data residency

To have checks validated and their results stored in the EU, point the client at the EU endpoint, https://api-eu.email-validation.org, with EMAILVAL_EU_BASE_URL:

#include <email_validation.h>
#include <stdlib.h>

emailval_client *eu_client(void) {
  emailval_client_options options;
  emailval_client_options_init(&options);
  options.base_url = EMAILVAL_EU_BASE_URL;
  emailval_client *client = NULL;
  emailval_client_new(getenv("EV_API_KEY"), &options, &client, NULL);
  return client;
}

Everything else stays the same; on the EU endpoint a check is always sent as a POST. See EU data residency.

Concurrency and cancellation

Calls are synchronous; run several at once from several threads with one shared client (keep within your rate limit; for long lists use a bulk job). To cancel a call, pass an emailval_cancel token in emailval_call_options and call emailval_cancel_request from any thread: the call stops at once, during a request, a retry or poll wait, or while you read a results stream, with EMAILVAL_ERR_CONNECTION and code aborted. A cancelled call is never retried.

#include <email_validation.h>

emailval_kind credits_cancellable(emailval_client *client, const emailval_cancel *token, emailval_credits **out) {
  emailval_call_options call = {0};
  call.cancel = token; /* another thread may call emailval_cancel_request */
  return emailval_credits_get(client, &call, out, NULL);
}

Handle errors

#include <email_validation.h>
#include <stdio.h>

void report(emailval_client *client, const char *address) {
  emailval_validation_result *r = NULL;
  emailval_error *err = NULL;
  emailval_kind kind = emailval_validate(client, address, NULL, NULL, &r, &err);
  if (kind == EMAILVAL_ERR_INSUFFICIENT_CREDITS) {
    printf("buy credits; nothing was charged\n");
  } else if (kind == EMAILVAL_ERR_RATE_LIMIT && err != NULL && err->has_retry_after) {
    printf("retry in %lld s\n", (long long)err->retry_after);
  } else if (kind != EMAILVAL_OK && err != NULL) {
    printf("%s %d %s %s\n", emailval_kind_name(kind), err->status, err->code,
           err->request_id != NULL ? err->request_id : "-");
  }
  emailval_validation_result_free(r);
  emailval_error_free(err);
}

Every function that sends a request or can fail returns an emailval_kind, EMAILVAL_OK on success (the constants lookups below, such as emailval_reasons_for_status(), return their value instead). The API's errors are EMAILVAL_ERR_INVALID_REQUEST, EMAILVAL_ERR_AUTHENTICATION, EMAILVAL_ERR_INSUFFICIENT_CREDITS, EMAILVAL_ERR_PERMISSION_DENIED, EMAILVAL_ERR_NOT_FOUND, EMAILVAL_ERR_CONFLICT, EMAILVAL_ERR_RATE_LIMIT, EMAILVAL_ERR_SERVICE_UNAVAILABLE, EMAILVAL_ERR_SERVER, EMAILVAL_ERR_CONNECTION and the base EMAILVAL_ERR_EMAIL_VALIDATION; the emailval_error has status (0 when no answer arrived), code, message, request_id and retry_after. A wrong argument is EMAILVAL_ERR_ARGUMENT before any request is sent. In C++, each kind is an exception class (RateLimitError, ConnectionError, ...) derived from email_validation::Error.

Retries and timeouts

#include <email_validation.h>
#include <stdlib.h>

emailval_client *tuned_client(void) {
  emailval_client_options options;
  emailval_client_options_init(&options);
  options.timeout = 30;        /* seconds per attempt; an idle timeout for uploads and downloads */
  options.max_retries = 2;     /* after 429 / 503 answers */
  options.max_retry_wait = 10; /* longer Retry-After values are returned to you */
  emailval_client *client = NULL;
  emailval_client_new(getenv("EV_API_KEY"), &options, &client, NULL);
  return client;
}

429 and 503 answers are retried with backoff, honoring Retry-After. A daily key cap is never retried, and a POST or DELETE (a validation, a bulk upload, start or cancel) is never sent again after a network failure or a timeout, because it may already have been charged or applied; only GET requests are retried then.

timeout (default 30 s) bounds each attempt, not the whole call. For JSON calls it covers the whole attempt: connecting, sending and reading the full answer. Transfers (a bulk create with a file, results, the results stream and download) use it as an idle timeout instead: the attempt fails only when no byte was sent or received for that long, so a slow but progressing upload or download of a large file is never cut, and no timer runs while your code is busy between two reads of a stream. A large upload gets up to 32 s more: while it is sent, and while its answer is awaited, that bound grows by the time the kernel's send buffer needs to drain (min(size, 4 MiB) at 128 KiB/s). request_timeout in emailval_call_options replaces the timeout for one call.

A bulk create that ends in a timeout or connection_error, or is cancelled (code aborted), may still have created the job: the service can receive the whole list after the SDK stopped waiting, and unless you set start to EMAILVAL_FALSE that job runs and is charged. The API has no job list and no idempotency key, so before you upload the list again, look for the job under Bulk lists in the dashboard (jobs created through the API are listed there too, as "Untitled list" with their upload time). A webhook cannot tell you: a job created through the API sends bulk.completed / bulk.failed only to the endpoint you passed as webhook_endpoint_id, and only when the job ends.

Credits, usage and status

#include <email_validation.h>
#include <stdio.h>

void account(emailval_client *client) {
  emailval_credits *credits = NULL; /* balance, debt, pending, lots */
  if (emailval_credits_get(client, NULL, &credits, NULL) == EMAILVAL_OK) {
    printf("%lld\n", (long long)credits->balance);
    emailval_credits_free(credits);
  }
  emailval_usage_options u = {0};
  u.group_by = EMAILVAL_GROUP_BY_DAY; /* from / to as YYYY-MM-DD for another range */
  emailval_usage *usage = NULL;
  if (emailval_usage_get(client, &u, NULL, &usage, NULL) == EMAILVAL_OK) {
    printf("%zu days\n", usage->day_count);
    emailval_usage_free(usage);
  }
  emailval_service_status *status = NULL;
  if (emailval_status_get(client, NULL, &status, NULL) == EMAILVAL_OK) {
    printf("%s\n", status->status);
    emailval_service_status_free(status);
  }
}

Usage covers the last 90 days by default.

Bulk jobs

#include <email_validation.h>
#include <stdio.h>

int bulk(emailval_client *client) {
  emailval_bulk_create_params params = {0};
  params.file_path = "list.csv"; /* or emails + email_count, file + file_len, or file_reader */
  params.column_name = "email";
  emailval_bulk_job *job = NULL;
  if (emailval_bulk_create(client, &params, NULL, &job, NULL) != EMAILVAL_OK) {
    return 1;
  }
  char id[32];
  snprintf(id, sizeof id, "%s", job->id);
  emailval_bulk_job_free(job);
  emailval_wait_options wait = {0};
  wait.interval = 5;
  if (emailval_bulk_wait(client, id, &wait, NULL, &job, NULL) != EMAILVAL_OK) {
    return 1;
  }
  int done = emailval_bulk_job_is_terminal(job);
  emailval_bulk_job_free(job);
  int64_t size = 0;
  return done && emailval_bulk_download(client, id, "results.csv", NULL, NULL, &size, NULL) == EMAILVAL_OK ? 0 : 1;
}

Create a job from a list (emails, sent as JSON), bytes in memory (file, sent from your buffer), a path or a stream (read into memory once: the API needs a length and retries send the same bytes). A job created with start = EMAILVAL_FALSE waits in ready until emailval_bulk_start: call start before wait. Wait returns once the job is completed, failed, cancelled or expired, or waits in ready with an error_code (it could not start); check its status before downloading. emailval_bulk_results returns the whole CSV, emailval_bulk_results_open streams it, and emailval_bulk_download writes it to a file. filter is all or one status and filters a non-empty list of statuses; anything else is refused before any request.

emailval_bulk_download streams into <path>.<job id>.<shape>.part beside path (the shape is the filter, columns and escaping, e.g. results.csv.job_….all-original-escaped.part) and moves it onto path when complete. A file already at path is replaced only then and is left as it was if the call fails, so the same path can be reused across jobs. With resume unset or EMAILVAL_TRUE, a failed download continues that job's own partial file with a Range request, never the file at path; EMAILVAL_FALSE starts over. Only filter all (or a single status of valid, invalid or unknown with escape_formulas = EMAILVAL_FALSE) with columns original can be resumed; other shapes start from byte 0. A second call after success downloads again. Run one download per destination at a time. A symbolic link at path is followed (its target is replaced) and a file at path keeps its permissions. A directory, FIFO or device at path is refused (EMAILVAL_ERR_ARGUMENT) before any request. After EMAILVAL_ERR_EMAIL_VALIDATION with unexpected_response, call again with resume = EMAILVAL_FALSE to start over. Leftover *.part files of other jobs or options beside the destination can be deleted.

Verify webhooks

#include <email_validation.h>
#include <stdlib.h>
#include <string.h>

/* body: the raw request body; headers: name/value pairs in any case */
int on_webhook(const char *body, size_t len, const emailval_header *headers, size_t count) {
  emailval_webhook_event *event = NULL;
  if (emailval_verify_webhook(body, len, headers, count, getenv("EV_WEBHOOK_SECRET"), NULL, &event, NULL) !=
      EMAILVAL_OK) {
    return 400;
  }
  if (event->type != NULL && strcmp(event->type, EMAILVAL_EVENT_BULK_COMPLETED) == 0) {
    const char *job_id = emailval_json_string(emailval_json_get(event->data, "job_id"), NULL);
    (void)job_id;
  }
  emailval_webhook_event_free(event);
  return 204;
}

event->type can also be webhook.test, sent when you press Send test in the dashboard: answer it with a 2xx like any other event. Deduplicate on the webhook-id header. emailval_sign_webhook creates a valid signature for local tests.

Constants

emailval_statuses(), emailval_reasons(), emailval_reasons_for_status(), emailval_reason_status(), emailval_reason_action(), emailval_actions(), emailval_error_codes(), emailval_webhook_events(), macros such as EMAILVAL_STATUS_VALID and EMAILVAL_REASON_ACCEPTED, and emailval_sandbox_address(reason, buf, size) mirror the API's vocabulary. See statuses and reasons and test addresses.

#include <email_validation.h>
#include <stdio.h>

void print_reasons(void) {
  emailval_string_list reasons = emailval_reasons_for_status(EMAILVAL_STATUS_VALID);
  for (size_t i = 0; i < reasons.count; i++) {
    printf("%s: %s\n", reasons.items[i], emailval_reason_action(reasons.items[i]));
  }
}

Bring your own HTTP client

The built-in transport is libcurl. To replace it (another HTTP library, a proxy layer, a fake in tests), fill an emailval_transport with send, read and close functions and pass it as options.transport. It must never follow redirects, send bodies with Content-Length, send every non-GET request on a fresh connection that is never reused and never resend anything by itself (otherwise a validation, bulk upload, start or cancel could be sent twice after a network failure, although it may already have been charged or applied), close a connection whose answer was not read to the end, and honor the timeout, transfer, drain and cancel fields of each request: C cannot interrupt a blocked call, so the SDK relies on the transport for those bounds. Do not give it a total timeout that would cut a slow but progressing transfer.

#include <email_validation.h>

static int my_send(void *ctx, const emailval_request *req, emailval_response *res) {
  (void)ctx;
  (void)req;
  emailval_response_set_status(res, 503); /* send req, then report the status, headers and a body handle */
  return EMAILVAL_TRANSPORT_OK;
}

static ptrdiff_t my_read(void *ctx, void *body, unsigned char *buf, size_t size) {
  (void)ctx;
  (void)body;
  (void)buf;
  (void)size;
  return 0;
}

static void my_close(void *ctx, void *body) {
  (void)ctx;
  (void)body;
}

const emailval_transport my_transport = {NULL, my_send, my_read, my_close, NULL};

Updated · Maintained by: Email Validation LLC

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