Skip to main content
The First Step
December 26, 2024
Question

Need some help with Fax API

Hello, 

I need some help with the fax api setting up. 
We want to integrate the fax api with our current Java/Node Environment.

Is there any instruction about that.

Thanks

 

    7 replies

    PhongVu
    Community Manager
    Community Manager
    December 27, 2024

    Check out this dev guide and see the sample code in your interested language.

    YaoThe First StepAuthor
    The First Step
    December 27, 2024

    Thank you very much!!

    Check out this dev guide and see the sample code in your interested language.

     

    YaoThe First StepAuthor
    The First Step
    December 27, 2024

    Check out this dev guide and see the sample code in your interested language.

    I keep getting this error, have no idea how to fix it? any help?

     

    PhongVu
    Community Manager
    Community Manager
    December 27, 2024

    It must be your environment. What programming language and how to you call the API? Share some code snippets and the app client id.

    YaoThe First StepAuthor
    The First Step
    December 27, 2024

    It must be your environment. What programming language and how to you call the API? Share some code snippets and the app client id.

    for the sms api, it ran successfully.

    for FaxAPI, it passed the auth, but then it failed.

     

     

    const RC = require('@ringcentral/sdk').SDK

    const path = require('path')

    require('dotenv').config()

     

    // Instantiate the SDK and get the platform instance

    var rcsdk = new RC({

    server: process.env.RC_SERVER_URL,

    clientId: process.env.RC_APP_CLIENT_ID,

    clientSecret: process.env.RC_APP_CLIENT_SECRET

    });

    var platform = rcsdk.platform();

     

    /* Authenticate a user using a personal JWT token */

    platform.login({ jwt: process.env.RC_USER_JWT })

     

    // For the purpose of testing the code, we put the recipient number in the variable.

    var RECIPIENT = process.env.SMS_RECIPIENT

     

    platform.on(platform.events.loginSuccess, function(e){

    send_fax()

    });

     

    platform.on(platform.events.loginError, function(e){

    console.log("Unable to authenticate to platform. Check credentials.", e.message)

    process.exit(1)

    });

     

    /*

    Send a high resolution fax message to a recipient number

    */

    async function send_fax() {

    try {

    const FormData = require('form-data');

    const fs = require('fs');

    const formData = new FormData();

    // Fix: Changed the JSON structure to match API requirements

    formData.append('json', JSON.stringify({

    to: [process.env.SMS_RECIPIENT] // Remove the phoneNumber object wrapper

    }));

    // Rest of the form data remains the same

    formData.append('attachment', fs.createReadStream('./fax.pdf'), {

    filename: 'fax.pdf',

    contentType: 'application/pdf'

    });

    let endpoint = "/restapi/v1.0/account/~/extension/~/fax";

    console.log("Sending fax to:", process.env.SMS_RECIPIENT);

    var resp = await platform.post(endpoint, formData);

    var jsonObj = await resp.json();

    console.log("Fax sent. Message id: " + jsonObj.id);

    // Add back the status check

    check_fax_message_status(jsonObj.id);

    } catch(e) {

    console.log("Error sending fax:", e.message);

    process.exit(1);

    }

    }


     

    /*

    Check the sending message status until it's out of the queued status

    */

    async function check_fax_message_status(messageId){

    try {

    let endpoint = `/restapi/v1.0/account/~/extension/~/message-store/${messageId}`

    let resp = await platform.get(endpoint);

    let jsonObj = await resp.json()

    console.log("Message status: ", jsonObj.messageStatus)

    if (jsonObj.messageStatus == "Queued"){

    await sleep (10000);

    check_fax_message_status(jsonObj.id);

    }

    } catch (e) {

    console.log(e.message)

    }

    }

     

    const sleep = async (ms) => {

    await new Promise(r => setTimeout(r, ms));

    }

     

     

    I am using the same credentials as sms api.

    here are some settings.

     

    Thanks!

    PhongVu
    Community Manager
    Community Manager
    December 27, 2024

    You passed the string instead of a buffer? Also the ‘to’ is an array of object with ‘phoneNumber’. Try this

    var FormData = require('form-data');
    formData = new FormData();
    var body = {
    to: [{'phoneNumber': RECIPIENT}],
    faxResolution: 'High',
    coverIndex: 0,
    }

    formData.append('json', new Buffer.from(JSON.stringify(body)), {
    contentType: 'application/json'
    });

    formData.append('attachment', require('fs').createReadStream('./fax.pdf'));
    var resp = await platform.post(endpoint, formData)

     

    YaoThe First StepAuthor
    The First Step
    December 27, 2024

    Thank you so much, it is working now.