Handle an incoming fax with Java
Now that you know how to send yourself a fax a using the Fax API, learn how to handle incoming faxes.
In this guide you will learn:
- How to set up your your Java app
- How to start your server
- About callbacks
- How to receive an incoming fax
What you need to know before you start
Before you can get started, you need the following already set up:
- Set all Fax API configuration settings.
- JDK 21 or later and a familiarity with how to create a new Java application.
- Gradle 8.4 or later and a familiarity with how use the Gradle build tools.
- ngrok. You'll use ngrok to open a tunnel to your local server.
Set up your Java application
Create a new folder where you want to keep your app project. Then, open a terminal or command prompt to that location.
Create a new Java application using Gradle with the following command:
gradle init
app
, and then accept the defaults for the rest of the options.Install your dependencies
We will be using Springboot to create a lightweight webserver that will listen for HTTP requests from the Sinch servers to handle incoming calls. We'll be using Lombok to deserialize requests.
In your project folder, navigate to the/app
folder and open the build.gradle
file. Replace all of the code in the file with the following code:plugins {
id 'application'
id 'org.springframework.boot' version '3.1.5'
id 'io.spring.dependency-management' version '1.1.4'
id 'java'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
application {
mainClass = 'app.App'
}
Save and close the file.
Modify your application
Open theApp.java
file in your project folder, located in \app\scr\main\java\app
, and populate that file with the "App.java" code found on this page and save the file.This file contains the logic to download fax content from the Sinch servers.
Start your server
At this point, you can start your server with the following command:
gradle run
Start your ngrok tunnel
Now you need to start your ngrok tunnel so that the Sinch servers can access the webserver running on your local machine. Run the following command to start your tunnel:
ngrok http 8080
This starts a tunnel to your webserver, which is running on port 8080 of your localhost.
Copy the http ngrok URL. Navigate to your Fax service on your dashboard. Select your default service and click Edit. You'll see a field labeled "Incoming webhook URL." Paste your ngrok URL into that field and click Save.
Now your server is listening and your incoming webhook URL is configured, so you're almost ready to test everything and send a fax that you can receive. But before we do, let's take a closer look at webhooks. If you already know about webhooks, skip right to sending yourself a fax.
Understanding webhooks
Webhooks (also known as "callbacks" or "notifications") are the method that the Fax API uses to inform you when your Sinch number is sent a fax. Basically, a notification is a request that the Sinch servers send to your server whenever something happens (otherwise known as an "event"), such as when you send or receive a fax. There are two different kinds of events, the Fax Completed and the Incoming Fax event, but the one we're concerned about is the Incoming Fax event.
An Incoming Fax event happens whenever someone sends a fax to one of your Sinch numbers. All of the notification events the Fax API sends expect a 200 OK response in return.
And that's it! Now we can test.
Send your Sinch number a fax
Using the app we created in the previous guide, send a fax to your Sinch number. The fax should be picked up by the Sinch servers and a PDF of your fax will be downloaded to your machine. Now you know how to handle an incoming fax.
Next steps
Learn more about the Fax API: