Skip to content

OkHttp с NordRouter

java
OkHttpClient client = new OkHttpClient();
MediaType json = MediaType.get("application/json; charset=utf-8");
String payload = """
  {"model":"anthropic/claude-sonnet-4.6",
   "messages":[{"role":"user","content":"Привет!"}]}
  """;

Request request = new Request.Builder()
    .url("https://nordrouter.com/v1/chat/completions")
    .header("Authorization", "Bearer " + System.getenv("NORDROUTER_API_KEY"))
    .post(RequestBody.create(payload, json))
    .build();

try (Response response = client.newCall(request).execute()) {
    String body = response.body().string();
    if (!response.isSuccessful()) throw new IOException(body);
    System.out.println(body);
}

Для Android не встраивайте постоянный коммерческий ключ в APK: его смогут извлечь. Используйте собственный backend, если приложение распространяется.

Все программы