Send a fax with Java

You can quickly see how the Fax API works by sending yourself a fax using the Fax API.

In this guide you will learn:
  1. How to set up your Java application.
  2. How to send a fax to your phone number.

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 8 or later and a familiarity with how to create a new Java application.
  • Gradle and a familiarity with how use the Gradle build tools.

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:

Copy
Copied
gradle init
In the prompts, select that you want to create an application, name your project and source package app, and then accept the defaults for the rest of the options.

Modify your application

Open the App.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.

This code sends a fax to a specified number using the Sinch Fax API.

To

In this example you want to fax a phone number. Change the value of the to parameter to the phone number you verified in your dashboard in E.164 format.
Note:

When your account is in trial mode, you can only send a fax to your verified numbers. If you want to fax any number, you need to upgrade your account!

Content URL

The Fax API can send either files directly or the URL of a web page or other online resource. For the purposes of this tutorial, use a web page because it's the easiest way to get started. Change the value of YOUR_content_url to the URL of the web page.
Note:

You can use any URL on the Internet (including ones with basic authentication), and we'll pull it down and make it a fax. This might be useful to you if you're using a web framework for templates and creating fax files.

If you are passing fax a secure URL (starting with https://), make sure that your SSL certificate (including your intermediate cert, if you have one) is installed properly, valid, and up-to-date.

Callback URL

If you provide a callback URL on your Fax service, the Fax API can send notification events to your server about your outgoing fax, such as when or if the fax was delivered. You can set that callback URL in your request body. Change the value of YOUR_callback_url to the URL of your server that's listening to incoming requests.

Fill in your parameters

Before you can run the code, you need to update some more values so you can connect to your Sinch account. Update the following parameters with your own values:

ParameterYour value
YOUR_project_idThe project ID found on your Sinch dashboard.
YOUR_access_keyThe access key found on your Sinch dashboard.
YOUR_access_secretThe access secret found on your Sinch dashboard. Note: Access secrets are only available during initial key creation.

Save the file.

Send your first fax

Now you can execute the code and send your fax. Run the following command:

Copy
Copied
gradle run

You should receive a fax to the number you specified with the web page that you configured.

Troubleshooting tip

If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.