Hey there, fellow developer! Ready to supercharge your SEO tools with some SEMrush magic? In this guide, we'll walk through building a robust SEMrush API integration in Java. SEMrush's API is a goldmine for SEO data, and we're about to tap into it. Let's dive in!
Before we start coding, make sure you've got:
First things first, let's set 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'
Now, let's create our SEMrushApiClient
class:
import okhttp3.*; import java.io.IOException; public class SEMrushApiClient { private final OkHttpClient client; private final String apiKey; private static final String BASE_URL = "https://api.semrush.com"; public SEMrushApiClient(String apiKey) { this.client = new OkHttpClient(); this.apiKey = apiKey; } // We'll add more methods here soon! }
Let's add a method to make GET requests:
public String get(String endpoint, Map<String, String> params) throws IOException { HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + endpoint).newBuilder(); urlBuilder.addQueryParameter("key", apiKey); params.forEach(urlBuilder::addQueryParameter); Request request = new Request.Builder() .url(urlBuilder.build()) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
Now, let's add some methods for specific SEMrush features:
public String getDomainOverview(String domain) throws IOException { Map<String, String> params = new HashMap<>(); params.put("type", "domain_ranks"); params.put("domain", domain); return get("/analytics/v1/", params); } public String getKeywordOverview(String keyword) throws IOException { Map<String, String> params = new HashMap<>(); params.put("type", "phrase_this"); params.put("phrase", keyword); return get("/analytics/v1/", params); } // Add more methods for other features you need
Don't forget to handle those pesky errors:
public String get(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); } return response.body().string(); } catch (IOException e) { System.err.println("Error making API request: " + e.getMessage()); throw e; } }
Time to put our client to the test:
public class SEMrushApiClientTest { @Test public void testGetDomainOverview() throws IOException { SEMrushApiClient client = new SEMrushApiClient("your-api-key"); String result = client.getDomainOverview("example.com"); assertNotNull(result); // Add more assertions based on expected response } // Add more tests for other methods }
To make our client even better:
Here's a quick example of async requests:
public CompletableFuture<String> getAsync(String endpoint, Map<String, String> params) { return CompletableFuture.supplyAsync(() -> { try { return get(endpoint, params); } catch (IOException e) { throw new CompletionException(e); } }); }
And there you have it! You've just built a solid SEMrush API integration in Java. With this foundation, you can now fetch domain analytics, research keywords, analyze backlinks, and much more. The SEO world is your oyster!
Remember, this is just the beginning. Feel free to extend the client with more features, improve error handling, or even build a full-fledged SEO tool around it. The possibilities are endless!
Happy coding, and may your SEO efforts be ever fruitful! 🚀📈