# Kotlin

MuxForms on Kotlin/Android POSTs application/json to the submit endpoint with the ingest secret in Authorization. Use coroutines on Dispatchers.IO for the network call from Compose or XML layouts.

## Kotlin

| Library | Role |
| --- | --- |
| OkHttp | POST body |
| kotlinx.serialization | JSON encode |
| BuildConfig | Avoid secret in APK—use backend |
| 422 | Show field errors in UI |

## MuxFormsClient.kt (OkHttp)

```
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

private val JSON = "application/json; charset=utf-8".toMediaType()

suspend fun submitContact(email: String, message: String, secret: String) {
  val body = JSONObject()
    .put("email", email)
    .put("message", message)
    .put("platform", "android")
    .put("_hp", "")
    .toString()
    .toRequestBody(JSON)

  val request = Request.Builder()
    .url("https://www.muxforms.com/api/v1/forms/FORM_UUID/submit")
    .post(body)
    .addHeader("Authorization", "Bearer $secret")
    .build()

  OkHttpClient().newCall(request).execute().use { response ->
    if (!response.isSuccessful) error("Submit failed: ${response.code}")
  }
}
```

## FAQ

### Jetpack Compose?

launch coroutine on button click; update UI state on result.

### ProGuard?

Keep data classes used for JSON if minifying.

### Wear OS?

Same API; shorter forms recommended.

### Multi-module?

Put submit client in :network module.

---

HTML version: https://www.muxforms.com/docs/kotlin
Source: MuxForms (https://www.muxforms.com)
