Back

Step by Step Guide to Building a Streak API Integration in Java

Aug 14, 20248 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your CRM game with Streak? You're in the right place. We're going to walk through building a Streak API integration in Java. Buckle up – it's going to be a fun ride!

Prerequisites

Before we dive in, make sure you've got:

  • A Java development environment (I know you've got this covered!)
  • A Streak API key (grab one from your Streak settings)
  • Your favorite HTTP client library (we'll use OkHttp in this guide)

Setting up the project

Let's kick things off by setting up our project:

  1. Create a new Java project in your IDE of choice.
  2. If you're using Maven, add this to your 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'

Authentication

Streak uses API key authentication. Let's create a base request method:

import okhttp3.*; public class StreakClient { private static final String BASE_URL = "https://www.streak.com/api/v1"; private final OkHttpClient client; private final String apiKey; public StreakClient(String apiKey) { this.apiKey = apiKey; this.client = new OkHttpClient(); } private Request.Builder getRequestBuilder(String endpoint) { return new Request.Builder() .url(BASE_URL + endpoint) .header("Authorization", Credentials.basic(apiKey, "")); } // We'll add more methods here soon! }

Core API Interactions

Pipelines

Let's fetch those pipelines:

public JSONArray getPipelines() throws IOException { Request request = getRequestBuilder("/pipelines").build(); try (Response response = client.newCall(request).execute()) { return new JSONArray(response.body().string()); } }

Creating a pipeline? Easy peasy:

public JSONObject createPipeline(String name) throws IOException { RequestBody body = new FormBody.Builder() .add("name", name) .build(); Request request = getRequestBuilder("/pipelines") .post(body) .build(); try (Response response = client.newCall(request).execute()) { return new JSONObject(response.body().string()); } }

Boxes (Deals)

Time to handle those boxes:

public JSONArray getBoxes(String pipelineKey) throws IOException { Request request = getRequestBuilder("/pipelines/" + pipelineKey + "/boxes").build(); try (Response response = client.newCall(request).execute()) { return new JSONArray(response.body().string()); } } public JSONObject createBox(String pipelineKey, String name) throws IOException { RequestBody body = new FormBody.Builder() .add("name", name) .build(); Request request = getRequestBuilder("/pipelines/" + pipelineKey + "/boxes") .post(body) .build(); try (Response response = client.newCall(request).execute()) { return new JSONObject(response.body().string()); } }

Stages

Moving boxes between stages? We've got you covered:

public JSONObject moveBoxToStage(String boxKey, String stageKey) throws IOException { RequestBody body = new FormBody.Builder() .add("stageKey", stageKey) .build(); Request request = getRequestBuilder("/boxes/" + boxKey) .put(body) .build(); try (Response response = client.newCall(request).execute()) { return new JSONObject(response.body().string()); } }

Handling Webhooks

Webhooks are your friends! Here's a quick servlet to handle them:

@WebServlet("/webhook") public class StreakWebhookServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedReader reader = request.getReader(); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } JSONObject payload = new JSONObject(sb.toString()); // Process the webhook payload here System.out.println("Received webhook: " + payload.toString()); response.setStatus(HttpServletResponse.SC_OK); } }

Error Handling and Rate Limiting

Let's add some retry logic and respect those rate limits:

private static final int MAX_RETRIES = 3; private static final int RETRY_DELAY_MS = 1000; private JSONObject executeWithRetry(Request request) throws IOException { for (int i = 0; i < MAX_RETRIES; i++) { try (Response response = client.newCall(request).execute()) { if (response.code() == 429) { // Rate limited, wait and retry Thread.sleep(RETRY_DELAY_MS * (i + 1)); continue; } return new JSONObject(response.body().string()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Request interrupted", e); } } throw new IOException("Max retries exceeded"); }

Testing the Integration

Don't forget to test! Here's a quick JUnit test to get you started:

import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class StreakClientTest { private final StreakClient client = new StreakClient("your-api-key"); @Test void testGetPipelines() throws IOException { JSONArray pipelines = client.getPipelines(); assertNotNull(pipelines); assertTrue(pipelines.length() > 0); } }

Best Practices

  1. Use batch operations when possible to reduce API calls.
  2. Implement caching for frequently accessed data.
  3. Always validate and sanitize input before sending it to the API.

Conclusion

And there you have it! You've just built a solid foundation for your Streak API integration in Java. Remember, this is just the beginning – there's so much more you can do with the Streak API. Keep exploring, keep coding, and most importantly, have fun with it!

Got questions? Hit up the Streak API docs or drop a line in the developer community. Now go forth and build something awesome! 🚀