Anthropic from Kotlin / Android
SDK examples · All languages and providers · API reference
The official Java SDK com.anthropic:anthropic-java works from Kotlin: you set the base URL and pass the user's token as authToken. Your Anthropic key stays on the server; the app holds only a token that expires in one hour.
1. Set up the service
In the dashboard, create an app, add a service and store your Anthropic key in it:
claude → https://api.anthropic.com
Set the base URL without /v1: the Anthropic API path is /v1/messages, so a service that ends in /v1 would produce /v1/v1/messages.
2. Get a token for the user
The app asks MyAppToken for a token with its app ID and your identifier for the user. The token lasts one hour; rate limits and analytics are tied to that user.
import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject const val API = "https://api.myapptoken.com/api/v1" const val APP_ID = "<app-id>" private val http = OkHttpClient() // Blocking: call it off the main thread, e.g. withContext(Dispatchers.IO). fun sessionToken(userId: String): String { val body = JSONObject().put("app_id", APP_ID).put("user_id", userId).toString() .toRequestBody("application/json".toMediaType()) val request = Request.Builder().url("$API/auth/simple").post(body).build() http.newCall(request).execute().use { res -> check(res.isSuccessful) { "auth failed: ${res.code}" } return JSONObject(res.body!!.string()).getString("access_token") } }
Renew it before it expires with POST /auth/refresh (send the current, still-valid token as Authorization: Bearer); if that fails, request a new one. With Firebase App Check enabled, also send the App Check header on proxy calls.
3. Call Anthropic
import com.anthropic.client.okhttp.AnthropicOkHttpClient import com.anthropic.models.messages.MessageCreateParams const val PROXY = "https://api.myapptoken.com/api/v1/proxy/<app-id>" // Before: .apiKey("sk-ant-…"). Here: authToken. This SDK cannot swap the // credential on an existing client, so build a new client after each // refresh; it is cheap. fun claude(jwt: String) = AnthropicOkHttpClient.builder() .authToken(jwt) .baseUrl("$PROXY/claude") .build() val msg = claude(jwt).messages().create( MessageCreateParams.builder() .model("claude-sonnet-5-5") .maxTokens(1024) .addUserMessage("Hello") .build())
These are blocking calls: run them off the main thread (for example withContext(Dispatchers.IO)) or use the SDK's async client.
Checked against the official Anthropic Java SDK 3.17: it sends /v1/messages under the base URL with Authorization: Bearer.
What changed
| Direct to the provider | Through MyAppToken | |
|---|---|---|
| Base URL | https://api.anthropic.com | https://api.myapptoken.com/api/v1/proxy/<app-id>/claude |
| Credential | provider key (sk-ant-…) | the user's token from /auth/simple |
More: Anthropic guide · rate limits · async jobs.
Start free — no credit card; the Free plan covers 10,000 requests a month.