Kotlin Examples
Kotlin code examples for integrating the nofakemails email validation API using OkHttp.
Dependencies
Add the following to your
build.gradle.kts:build.gradle.kts
// build.gradle.kts
dependencies {
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.google.code.gson:gson:2.10.1")
}Single Email Validation
Validate a single email address with a GET request to the /v1/email/{email} endpoint.
SingleValidation.kt
import okhttp3.OkHttpClient
import okhttp3.Request
fun validateEmail(email: String): String {
val client = OkHttpClient()
val apiKey = System.getenv("NOFAKEMAILS_API_KEY")
val request = Request.Builder()
.url("https://api.nofakemails.com/v1/email/$email")
.addHeader("X-API-Key", apiKey)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw RuntimeException("API request failed: ${response.code}")
}
return response.body?.string() ?: throw RuntimeException("Empty response body")
}
}
fun main() {
val result = validateEmail("test@example.com")
println(result)
}Response:
{
"email": "test@example.com",
"valid": true,
"normalized": "test@example.com",
"local_part": "test",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.10,
"domain": "example.com",
"mx": true,
"disposable": false,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
}See the Response Format page for a full description of every field.
Batch Validation
Validate multiple email addresses with a POST request to the /v1/emails endpoint. Accepts up to 64 emails per request.
BatchValidation.kt
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import com.google.gson.Gson
import com.google.gson.JsonObject
data class BatchRequest(val emails: List<String>)
fun validateBatch(emails: List<String>): String {
val client = OkHttpClient()
val apiKey = System.getenv("NOFAKEMAILS_API_KEY")
val gson = Gson()
val jsonBody = gson.toJson(BatchRequest(emails))
val requestBody = jsonBody.toRequestBody("application/json".toMediaType())
val request = Request.Builder()
.url("https://api.nofakemails.com/v1/emails")
.post(requestBody)
.addHeader("X-API-Key", apiKey)
.addHeader("Content-Type", "application/json")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw RuntimeException("Batch validation failed: ${response.code}")
}
return response.body?.string() ?: throw RuntimeException("Empty response body")
}
}
fun main() {
val emails = listOf(
"user1@example.com",
"user2@gmail.com",
"test@tempmail.com"
)
val json = validateBatch(emails)
val gson = Gson()
val batch = gson.fromJson(json, JsonObject::class.java)
// Check for partial results (timeout)
if (batch.get("partial").asBoolean) {
println("Warning: some emails may not have been processed")
}
println("Total: ${batch.get("total")}, Valid: ${batch.get("valid_count")}")
for (item in batch.getAsJsonArray("results")) {
val obj = item.asJsonObject
val status = obj.get("status").asString
if (status == "error") {
val error = obj.getAsJsonObject("error")
println("${obj.get("email").asString}: ERROR - ${error.get("message").asString}")
continue
}
val result = obj.getAsJsonObject("result")
println("${result.get("email").asString}: " +
"valid=${result.get("valid")}, disposable=${result.get("disposable")}")
}
}Response:
{
"success": true,
"partial": false,
"total": 3,
"valid_count": 3,
"invalid_count": 0,
"error_count": 0,
"usage_points": 3,
"processing_time_ms": 198,
"results": [
{
"email": "user1@example.com",
"status": "success",
"result": {
"email": "user1@example.com",
"valid": true,
"normalized": "user1@example.com",
"local_part": "user1",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.11,
"domain": "example.com",
"mx": true,
"disposable": false,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
},
{
"email": "user2@gmail.com",
"status": "success",
"result": {
"email": "user2@gmail.com",
"valid": true,
"normalized": "user2@gmail.com",
"local_part": "user2",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.10,
"domain": "gmail.com",
"mx": true,
"disposable": false,
"public": true,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
},
{
"email": "test@tempmail.com",
"status": "success",
"result": {
"email": "test@tempmail.com",
"valid": true,
"normalized": "test@tempmail.com",
"local_part": "test",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.10,
"domain": "tempmail.com",
"mx": true,
"disposable": true,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
}
]
}Each item in results contains the full validation result. See the Batch Validation response format for details on the envelope fields and per-email result structure.
Batch Limits
Maximum 64 emails per batch request. Duplicates are automatically deduplicated. See the Error Codes page if you encounter issues.