> For the complete documentation index, see [llms.txt](https://docs.sunlicense.hapangama.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sunlicense.hapangama.com/api-documentation/java-api.md).

# Java API

### Prerequisites

* Java 8 or higher
* SunLicense Server API URL
* Product ID

### Project Setup

#### 1. Configure Maven Repository

Add the Sundevs repository to your `pom.xml`:

```xml
<repositories>
    <repository>
        <id>sundevs</id>
        <name>Sundevs Repository</name>
        <url>https://repo.hapangama.com/releases</url>
    </repository>
</repositories>
```

#### 2. Add Dependency

Include the SunLicenseAPI dependency in your `pom.xml`:

```xml
<dependencies>
    <dependency>
        <groupId>com.hapangama</groupId>
        <artifactId>SunLicenseAPI</artifactId>
        <version>1.0.3</version>
    </dependency>
</dependencies>
```

### Basic Integration

#### 1. Initialize the API

Create an instance of SunLicenseAPI with your license details:

```java
SunLicenseAPI api = SunLicenseAPI.getLicense(
    "YOUR-LICENSE-KEY",    // License key
    YOUR_PRODUCT_ID,       // Product ID (integer)
    "YOUR_VERSION",        // Product version
    "YOUR_API_URL"         // API endpoint
);
```

{% hint style="warning" %}
License Key cannot be hardcoded its different from license to license and **you have to write a logic to take license key from a config file** and pass as a parameter.
{% endhint %}

#### 2. Implement License Validation

Add basic license validation to your application:

```java
try {
    api.validate();
    System.out.println("License is valid");
} catch (IOException e) {
    System.out.println("License validation failed: " + e.getMessage());
    System.exit(1);
}
```

### Advanced Configuration

#### Custom IP Address Configuration

You can manually set the IP address for validation:

```java
// Option 1: Get public IP automatically
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.ipify.org"))
    .GET()
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String publicIP = response.body();
api.setIp(publicIP);

// Option 2: Set IP manually
api.setIp("YOUR.IP.ADDRESS");
```

#### Loading License from File

For production environments, load the license key from a file:

```java
SunLicenseAPI api = SunLicenseAPI.getLicenseFromFile(
    "path/to/license.txt",
    YOUR_PRODUCT_ID,
    "YOUR_VERSION",
    "YOUR_API_URL"
);
```

\
**Custom HWID**\
\
You can write your own implementation for HWID and pass it with the request.

```java
api.setHwid("YOUR-CUSTOM-HWID");
```

### Troubleshooting

Common issues and solutions:

1. **Connection Issues**
   * Verify API endpoint URL
   * Check network connectivity
   * Confirm firewall settings
2. **Validation Failures**
   * Verify license key format
   * Check product ID is correct
   * Confirm IP address restrictions
3. **Configuration Problems**
   * Ensure Java version compatibility
   * Check dependency versions

Remember to replace placeholder values (`YOUR-LICENSE-KEY`, `YOUR_PRODUCT_ID`, `YOUR_API_URL`) with your actual values from your SunLicense account.
