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

# Working with the API

Sonar is supposed to be an easy-to-use plugin; therefore, the API has been designed to give a developer almost full control over the behavior of the plugin.

### Using the API

Before you are actually able to use the API, you will have to add the dependency to your project.&#x20;

```gradle
maven(url = "https://repo.jonesdev.xyz/releases/")
```

{% hint style="danger" %}
Please make sure you do not include the API in your built Jar file by using `compileOnly` instead of `implementation`.
{% endhint %}

```gradle
compileOnly("xyz.jonesdev.sonar:sonar-api:2.1.46")
```

Now you are able to access all the main API functions by using `Sonar.get()`. Replace `2.1.46` with the version of Sonar you are using.

### Events

Using Sonar's built-in event system is also not hard. First, you need to create an event listener:

```java
import xyz.jonesdev.sonar.api.event.SonarEvent;
import xyz.jonesdev.sonar.api.event.SonarEventListener;
import xyz.jonesdev.sonar.api.event.impl.UserVerifySuccessEvent;

public final class TestListener implements SonarEventListener {

  @Override
  public void handle(final SonarEvent event) {
    if (event instanceof UserVerifySuccessEvent successEvent) {
      // ... some example code ...
      System.out.printf("Test: %s (%s) (took %d ms to verify)%n",
        successEvent.getUsername(), successEvent.getOfflineUuid(), successEvent.getTimeTakenToVerify());
      System.out.println("You can also get the raw user data: " + successEvent.getUser());
    }
    // ...
  }
}
```

Then, you need to register your event listener using the Sonar API:

```java
Sonar.get().getEventManager().registerListener(new TestListener());
```

Have fun developing!
