Hey there, fellow developer! Ready to dive into the world of PagerDuty API integration? Let's roll up our sleeves and get coding!
PagerDuty's API is a powerful tool that allows you to programmatically manage incidents, schedules, and more. In this guide, we'll walk through building a Java integration that'll make your life easier and your incident management smoother.
Before we jump in, make sure you've got:
First things first, let's get our project set up:
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'
Alright, let's get that API key working for us:
private static final String API_KEY = "your_api_key_here"; private static final String BASE_URL = "https://api.pagerduty.com"; OkHttpClient client = new OkHttpClient(); Request.Builder requestBuilder = new Request.Builder() .header("Authorization", "Token token=" + API_KEY) .header("Accept", "application/vnd.pagerduty+json;version=2");
Time to make our first request! Let's fetch some incidents:
Request request = requestBuilder .url(BASE_URL + "/incidents") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } catch (IOException e) { e.printStackTrace(); }
Now, let's create an incident:
String json = "{\"incident\":{\"type\":\"incident\",\"title\":\"The server is on fire!\",\"service\":{\"id\":\"YOUR_SERVICE_ID\",\"type\":\"service_reference\"}}}"; RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8")); Request request = requestBuilder .url(BASE_URL + "/incidents") .post(body) .build(); // Execute the request similar to the GET example
Updating an incident is just as easy:
String incidentId = "YOUR_INCIDENT_ID"; String json = "{\"incident\":{\"type\":\"incident\",\"status\":\"resolved\"}}"; RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8")); Request request = requestBuilder .url(BASE_URL + "/incidents/" + incidentId) .put(body) .build(); // Execute the request
If you want to receive real-time updates, set up a webhook endpoint in your application and configure it in PagerDuty. Here's a basic servlet example:
@WebServlet("/pagerduty-webhook") public class PagerDutyWebhook extends HttpServlet { 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); } String payload = sb.toString(); // Process the webhook payload System.out.println("Received webhook: " + payload); response.setStatus(HttpServletResponse.SC_OK); } }
Don't forget to handle those pesky errors:
try { // Your API call here } catch (IOException e) { logger.error("API call failed", e); } catch (JsonParseException e) { logger.error("Failed to parse JSON response", e); }
Always test your code! Here's a simple JUnit test to get you started:
@Test public void testGetIncidents() { // Your test code here assertNotNull(response); assertEquals(200, response.code()); }
And there you have it! You've just built a solid foundation for your PagerDuty API integration in Java. From here, you can expand on this to create a full-fledged incident management system tailored to your needs.
Remember, the best code is the one that solves real problems. So go forth and make on-call life a little bit easier for everyone!
Happy coding, and may your servers always be up! 🚀