> ## Documentation Index
> Fetch the complete documentation index at: https://idto.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK token with Java

> Mint an SDK token on your server (Java 17 with only the JDK, as a single file) and open the idto verification flow in the browser.

Mint a short-lived SDK token on your server and hand it to the Web SDK in the browser. Your API key never leaves the server. Stack: Java 17 with only the JDK, as a single file.

**1. Set your credentials**

Put your keys in environment variables. Use `https://dev.idto.ai` (staging) while you build and `https://prod.idto.ai` (production) when you go live. Keep the client ID and API key on your server only. Never ship them to a browser or a mobile app.

```bash .env theme={null}
IDTO_BASE_URL=https://dev.idto.ai
IDTO_CLIENT_ID=your-client-id
IDTO_API_KEY=your-api-key
```

```bash theme={null}
set -a; source .env; set +a
```

**2. Install**

```bash theme={null}
java -version
```

**3. Add the token endpoint**

`POST /api/idto/token` calls `POST /auth/sdk/token` with your client ID and API key and returns only the token and its lifetime to the browser.

```java TokenServer.java theme={null}
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

public class TokenServer {
    public static void main(String[] args) throws Exception {
        String baseUrl = System.getenv().getOrDefault("IDTO_BASE_URL", "https://dev.idto.ai");
        String payload = String.format("{\"client_id\":\"%s\",\"client_secret\":\"%s\"}",
                System.getenv("IDTO_CLIENT_ID"), System.getenv("IDTO_API_KEY"));
        HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();

        HttpServer server = HttpServer.create(new InetSocketAddress(3000), 0);
        server.createContext("/api/idto/token", exchange -> {
            if (!"POST".equals(exchange.getRequestMethod())) {
                exchange.sendResponseHeaders(405, -1);
                return;
            }
            int status;
            String body;
            try {
                HttpRequest request = HttpRequest.newBuilder(URI.create(baseUrl + "/auth/sdk/token"))
                        .timeout(Duration.ofSeconds(15))
                        .header("Content-Type", "application/json")
                        .POST(HttpRequest.BodyPublishers.ofString(payload))
                        .build();
                HttpResponse<String> idto = client.send(request, HttpResponse.BodyHandlers.ofString());
                status = idto.statusCode() == 200 ? 200 : 502;
                body = idto.statusCode() == 200 ? idto.body() : "{\"error\":\"token request failed\"}";
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                status = 502;
                body = "{\"error\":\"token request failed\"}";
            }
            byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
            exchange.getResponseHeaders().set("Content-Type", "application/json");
            exchange.sendResponseHeaders(status, bytes.length);
            exchange.getResponseBody().write(bytes);
            exchange.close();
        });
        server.start();
        System.out.println("listening on http://localhost:3000");
    }
}
```

**4. Open the verification flow**

The page asks your server for a token and opens the flow. Use the workflow template ID from your dashboard, and drop `env: "development"` in production.

```html public/index.html theme={null}
<script src="https://idto-sdk-bucket.s3.ap-south-1.amazonaws.com/sdk/prod/idto.js"></script>
<button id="verify">Verify identity</button>
<script>
  document.getElementById("verify").addEventListener("click", async () => {
    const response = await fetch("/api/idto/token", { method: "POST" });
    const { access_token } = await response.json();
    IDtoSDK.open({
      client_token: access_token,
      workflow_template_id: "YOUR_WORKFLOW_TEMPLATE_ID",
      env: "development",
      onWorkflowComplete: (data) => console.log("session", data.session_token),
      onError: (data) => console.error(data),
    });
  });
</script>
```

This server uses the JDK `HttpServer` and `HttpClient`, so there is nothing to install.

**5. Run and check**

```bash theme={null}
java TokenServer.java
```

```bash theme={null}
curl -X POST http://localhost:3000/api/idto/token
```

```json 200 theme={null}
{"access_token": "<sdk-token>", "expires_in": 900}
```

## Good to know

* The SDK token lasts 900 seconds. Mint a new one for each verification instead of caching it.
* Minting a token is not charged. Verifications that run inside the flow are charged as usual.
* A `401` from `/auth/sdk/token` means the client ID or API key is wrong, or the client is inactive. Your endpoint returns `502` in that case so the browser never sees the upstream detail.
* To confirm the result from your server after `onWorkflowComplete`, see [Confirm a session result from your server](https://idto.mintlify.site/recipes/confirm-session-result.md).

Was this page helpful? Email us at [tech@idto.ai](mailto:tech@idto.ai?subject=Docs%20feedback%3A%20SDK%20token%20with%20Java) with the page link.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.