Using the API with Java - HypixelDev/PublicAPI GitHub Wiki

This page provides examples of how to use the Hypixel PublicAPI in a Java program. It is assumed that you have already configured your IDE and downloaded PublicAPI on your own machine.

Contents

Prerequisites

In order to follow along with this tutorial, you will need the following:

  • A live internet connection
  • A licensed copy of Minecraft installed on your local machine
  • A Java IDE (we will be using IntelliJ for this tutorial)
  • Maven (if not already built into your IDE)

Getting your API Key

To use the PublicAPI, you will first need to have an API key. The Hypixel API uses keys to keep track of how frequently a user is sending requests, as well as to determine if your API key is compromised.

To obtain your personal API key, you must own a valid Minecraft account and have the Minecraft Launcher installed.

  1. Launch Minecraft on release 1.8.0 or later (snapshot versions will not work)
  2. Open the Multiplayer GUI and connect to mc.hypixel.net
  3. Once you are connected, open up the chat bar (this can be done by pressing the T key by default)
  4. In the chat bar, type /api new and press enter. You should receive a message in chat saying, Your new API key is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (with your API key in place of the x's)
  5. When you click on that message, your API key should be copied to your clipboard. Paste that somewhere safe for later

NOTE: Your API key is to be treated just like a password. You should never share your API key with anyone, or else it could be used for malicious purposes.


Setting Up your Workspace

To start using the Java API, you will first need to open up a Java IDE of your choice. For this tutorial, we will be using IntelliJ IDEA (2020.1.1).

1. Firstly, you will need to create a new project. In IntelliJ, there is a button for this on the welcome screen:
"Create New Project" button

2. Then, select the Maven project option and click Next:
Maven project

3. Type in the name of your project and press the Finish button. This will create your new Maven project with that name:
Set project name & finish

4. In the Project window that appears, click on the folder with your project's name and double-click the pom.xml file:
Open pom.xml

5. At the end of this file (before the </project> tag), paste the following:

<repositories>
  <repository>
    <id>Hypixel</id>
    <url>https://repo.hypixel.net/repository/Hypixel/</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>net.hypixel</groupId>
    <artifactId>HypixelAPI</artifactId>
    <version>3.0.0</version>
  </dependency>
</dependencies>

NOTE: If some of the text is colored red, you may need to use the Load Maven Changes button in the top right (see screenshot)
Hypixel API pom dependency

If all goes well, you now should be able to use the Hypixel API in your very own Java project! See below for more information on using the API in various projects.


Simple Project - Check a Player's Stats

Once you've added the PublicAPI dependency to your Maven project, you can finally start using it. To show off some of the basics of the PublicAPI, I will be making a simple command-line stats checker.

First, go to your src/main/ folder (automatically created by IntelliJ) and then right click on the java folder. Click on New > Package and then type in the name of the package you would like to use for your project (see here for info on naming Java packages). For this example, I'll create the org.example.wikiexample package to reflect my Maven project's groupId.

Again, right click on your newly-created package and go to New > Java Class. You can name this class whatever you'd like, but I'll stick with WikiExample. Your Project Tree should now look something like this:
example project tree

The following code can go in the class you just created. Make sure that you replace <YOUR_PACKAGE_NAME> with the package you created earlier, as well as <YOUR_CLASS_NAME> with whatever you named your class, and YOUR_API_KEY with your Hypixel API key (see here for more info). It is highly recommended that you read through all of the included comments to ensure that you understand how the program works.

[CLICK HERE TO VIEW THE SAMPLE PROGRAM]

Once you make the changes mentioned above, a little Run icon should appear next to the line with the main method. You can click this button to run the program.
run button
Once the program is running, a terminal should appear where you can type in a Minecraft username and get some of their basic stats on Hypixel (if they have logged in before).
sample execution

And there it is! A very simple (albeit verbose) program that uses the Hypixel API to get a player's stats. Pretty cool, huh? Keep reading for more practical uses of the Hypixel API. [coming soon; for now, see here for various examples].

TODO

  • Add example usage in a Forge mod
  • Add example usage in a Bukkit plugin
⚠️ **GitHub.com Fallback** ⚠️