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

# Java

> Use the official KeyPort Java client to validate license keys and retrieve license details.

## Installation

Add the `keyport-java` dependency to your project:

<CodeGroup>
  ```xml Maven theme={"theme":{"light":"github-light","dark":"github-dark"}}
  <dependency>
    <groupId>sbs.keyport</groupId>
    <artifactId>keyport-java</artifactId>
    <version>1.0.1</version>
  </dependency>
  ```
</CodeGroup>

## Setup and usage

<Steps>
  <Step title="Initialize the client">
    Create a `KeyPort` instance with your API key, or load it from a config file:

    <CodeGroup>
      ```java API key in code theme={"theme":{"light":"github-light","dark":"github-dark"}}
      KeyPort client = new KeyPort("kp_live_xxx");
      ```

      ```java Config file theme={"theme":{"light":"github-light","dark":"github-dark"}}
      KeyPort client = KeyPort.fromConfigFile(Path.of("keyport.json"));
      ```
    </CodeGroup>
  </Step>

  <Step title="Validate a license key">
    Call `client.validate` with the license key you want to check:

    ```java theme={"theme":{"light":"github-light","dark":"github-dark"}}
    ValidateResponse response = client.validate("ABCD-EFGH-IJKL-MNOP", null, null);
    ```

    This sends a `POST /api/v1/validate` request and returns a `ValidateResponse`.
  </Step>

  <Step title="Retrieve license details">
    Call `client.getLicense` with the license key to fetch full license details:

    ```java theme={"theme":{"light":"github-light","dark":"github-dark"}}
    LicenseDetailResponse detail = client.getLicense("ABCD-EFGH-IJKL-MNOP");
    ```

    This sends a `GET /api/v1/license/:key` request and returns a `LicenseDetailResponse`.
  </Step>
</Steps>

## Full examples

<CodeGroup>
  ```java API key in code theme={"theme":{"light":"github-light","dark":"github-dark"}}
  KeyPort client = new KeyPort("kp_live_xxx");

  // Validate a license key
  ValidateResponse response = client.validate("ABCD-EFGH-IJKL-MNOP", null, null);

  // Retrieve full license details
  LicenseDetailResponse detail = client.getLicense("ABCD-EFGH-IJKL-MNOP");
  ```

  ```java Config file theme={"theme":{"light":"github-light","dark":"github-dark"}}
  KeyPort client = KeyPort.fromConfigFile(Path.of("keyport.json"));

  // Retrieve full license details
  LicenseDetailResponse detail = client.getLicense("ABCD-EFGH-IJKL-MNOP");
  ```
</CodeGroup>

<Tip>
  Store your API key in an environment variable or a `keyport.json` config file rather than hard-coding it in your source code.
</Tip>
