Hey there, fellow developer! Ready to supercharge your SEO tools with some Ahrefs data? You're in the right place. We're going to walk through building an Ahrefs API integration in Java. It's easier than you might think, and by the end of this guide, you'll be pulling valuable SEO insights like a pro.
Before we dive in, make sure you've got:
Let's kick things off by setting up our project:
pom.xml
:<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency>
For Gradle users, pop this into your build.gradle
:
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
First things first, let's get that authentication sorted:
public class AhrefsClient { private static final String API_ENDPOINT = "https://api.ahrefs.com/v1"; private final String apiKey; private final OkHttpClient client; public AhrefsClient(String apiKey) { this.apiKey = apiKey; this.client = new OkHttpClient(); } // We'll add more methods here soon! }
Now, let's create a method to make those API calls:
private String makeRequest(String endpoint, Map<String, String> params) throws IOException { HttpUrl.Builder urlBuilder = HttpUrl.parse(API_ENDPOINT + endpoint).newBuilder(); urlBuilder.addQueryParameter("token", apiKey); for (Map.Entry<String, String> param : params.entrySet()) { urlBuilder.addQueryParameter(param.getKey(), param.getValue()); } Request request = new Request.Builder() .url(urlBuilder.build()) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
We're getting JSON back, so let's parse it. Add this dependency for Gson:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency>
Now, let's create a method to parse the JSON:
private <T> T parseResponse(String json, Class<T> classOfT) { Gson gson = new Gson(); return gson.fromJson(json, classOfT); }
Let's implement a method for getting backlinks data:
public BacklinksData getBacklinksData(String target) throws IOException { Map<String, String> params = new HashMap<>(); params.put("target", target); params.put("mode", "domain"); params.put("output", "json"); String json = makeRequest("/backlinks", params); return parseResponse(json, BacklinksData.class); }
You'll need to create a BacklinksData
class to match the JSON structure.
Let's add some basic error handling and respect those rate limits:
private String makeRequest(String endpoint, Map<String, String> params) throws IOException { // ... previous code ... try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } // Simple rate limiting - wait a second between requests Thread.sleep(1000); return response.body().string(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Request interrupted", e); } }
Our AhrefsClient
class is shaping up nicely! Here's what we've got so far:
public class AhrefsClient { private static final String API_ENDPOINT = "https://api.ahrefs.com/v1"; private final String apiKey; private final OkHttpClient client; private final Gson gson; public AhrefsClient(String apiKey) { this.apiKey = apiKey; this.client = new OkHttpClient(); this.gson = new Gson(); } public BacklinksData getBacklinksData(String target) throws IOException { Map<String, String> params = new HashMap<>(); params.put("target", target); params.put("mode", "domain"); params.put("output", "json"); String json = makeRequest("/backlinks", params); return parseResponse(json, BacklinksData.class); } // ... other methods ... }
Let's put our client to work:
AhrefsClient client = new AhrefsClient("your-api-key"); try { BacklinksData data = client.getBacklinksData("example.com"); System.out.println("Backlinks: " + data.getBacklinks()); } catch (IOException e) { e.printStackTrace(); }
Don't forget to test! Here's a simple JUnit test to get you started:
public class AhrefsClientTest { @Test public void testGetBacklinksData() throws IOException { AhrefsClient client = new AhrefsClient("your-api-key"); BacklinksData data = client.getBacklinksData("example.com"); assertNotNull(data); assertTrue(data.getBacklinks() > 0); } }
And there you have it! You've just built a solid foundation for an Ahrefs API integration in Java. From here, you can expand on this client, add more endpoints, and start pulling in all sorts of valuable SEO data.
Remember, the Ahrefs API is powerful, so use it wisely. Keep an eye on your rate limits, and happy coding!