You can quickly see how the Voice API works by calling yourself using the API.
Before you can get started, you need the following already set up:
- Set all Fax API configuration settings.
- Python and a familiarity with how to create a new file.
- PIP (package installer for Python) and a familiarity with how to install Python modules.
We'll be using the requests
module to make HTTP requests. Open a command prompt and use the following command to install the requests
module:
pip install requests
Create a new file named send-fax.py
and paste the provided "send-fax.py" code found on this page into the file.
This tutorial uses basic authentication for testing purposes. We recommend using OAuth token-based authentication for production.
#Use this code to send a fax using the Fax API.
# Find the Sinch phone number assigned to your app
# and your access key and secret
# at dashboard.sinch.com/settings/access-keys
import requests
key = "YOUR_access_key"
secret = "YOUR_access_secret"
project_id = "YOUR_project_id"
to = "YOUR_to_number"
content_url = "YOUR_content_url"
callback_url = "YOUR_callback_url"
url = "https://fax.api.sinch.com/v3/projects/" + project_id + "/faxes"
payload = {
"to": to,
"contentUrl": content_url,
"callbackUrl": callback_url
}
headers = { "Content-Type": "application/json" }
response = requests.post(url, json=payload, headers=headers, auth=(key, secret))
data = response.json()
print(data)
This code sends a fax to a specified number using the Sinch Fax API.
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.
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!
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.
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.
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.
Before you can run the code, you need to update some more values in the Index.js
file so you can connect to your Sinch account. Update the following parameters with your own values:
Parameter | Your value |
---|---|
YOUR_project_id | The project ID found on your Sinch dashboard. |
YOUR_access_key | The access key found on your Sinch dashboard. |
YOUR_access_secret | The access secret found on your Sinch dashboard. Note: Access secrets are only available during initial key creation. |
Save the file.
Now you can execute the code and send your fax. Run the following command:
python send-fax.py
You should receive a fax to the number you specified with the web page that you configured.
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.