News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Retrieve fax image,How do I retrieve contents of fax via api
Tags: rest api
Jun 10, 2021 at 12:02pm   •   3 replies  •  0 likes
Andrew Prokop

Hi all. I am using the javascript SDK to retrieve a fax. I call the following:

var uri = 'https://media.devtest.ringcentral.com/restapi/v1.0/account/296784004/extension/296784004/message-store/9609075004/content/9609075004'; // discovered via get all faxes API

var resp = await platform.get(uri)

I believe I have now received the binary of the fax image. How do I save that to a file?


3 Answers
answered on Jun 11, 2021 at 5:43am  

Thank you! That did the trick.


 0
answered on Jun 10, 2021 at 6:07pm  

Thank you. I can retrieve all the records and "pdf" files are created. However, each one contains the following:


{

"errorCode" : "AGW-401",

"message" : "Authorization header is not specified",

"errors" : [ {

"errorCode" : "AGW-401",

"message" : "Authorization header is not specified"

} ]

}


I am new to RingCentral programming, but it appears that the token is added to contentUri. Perhaps this isn't working because of my authentication scheme and the way I created my application. I created the application as Other Non-UI (e.g. cronjob, daemon, command line) .

I do this in my code:

var rcsdk = new RC({

server: RINGCENTRAL_SERVER,

clientId: RINGCENTRAL_CLIENTID,

clientSecret: RINGCENTRAL_CLIENTSECRET

});

var platform = rcsdk.platform();


This works to retrieve the records, but not to retrieve the contents.

Any suggestions?




 0
on Jun 10, 2021 at 7:26pm   •  0 likes

Try this then. Remember to take care of API rate limit!

https://medium.com/ringcentral-developers/ringcentral-api-rate-limit-explained-2280fe53cb16

var async = require('async')
var fs = require('fs')

async function read_message_store_fax(){
  try {
    var resp = await platform.get('/restapi/v1.0/account/~/extension/~/message-store', {
          dateFrom: '2021-01-01T00:00:00.000Z',
          direction: ['Inbound'],
          messageType: ['Fax']
        })
    var jsonObj = await resp.json()
    for (var record of jsonObj.records){
      console.log(record)
      if (record.hasOwnProperty('attachments')){
        async.each(record.attachments,
          async function(attachment){
              var fileName = attachment.id + "_fax_attachment"
              var fileNameExt = attachment.contentType.split("/")
              fileName += "." + fileNameExt[1]
              var res = await platform.get(attachment.uri)
              var buffer = await res.buffer();
              fs.writeFileSync(fileName, buffer)
            })
      }
    }
  }catch (e){
    console.log(e.message)
  }
}
answered on Jun 10, 2021 at 5:02pm  

Use this sample code

var async = require('async')
var fs = require('fs')
var https = require('https');

async function read_message_store_fax(){
  try {
    var resp = await platform.get('/restapi/v1.0/account/~/extension/~/message-store', {
          dateFrom: '2021-01-01T00:00:00.000Z',
          direction: ['Inbound'],
          messageType: ['Fax']
        })
    var jsonObj = await resp.json()
    for (var record of jsonObj.records){
      console.log(record)
      if (record.hasOwnProperty('attachments')){
        async.each(record.attachments,
          function(attachment, callback){
              var fileName = attachment.id + "_fax_attachment"
              var fileNameExt = attachment.contentType.split("/")
              fileName += "." + fileNameExt[1]
              download_content(attachment.uri, fileName)
              callback()
            })
      }
    }
  }catch (e){
    console.log(e.message)
  }
}

async function download_content(contentUri, fileName){
    var uri = platform.createUrl(contentUri, {addToken: true});
    download(uri, fileName, function(){
      console.log("Save atttachment to the local machine.")
    })
}

const download = function(uri, dest, cb) {
    var file = fs.createWriteStream(dest);
    var request = https.get(uri, function(response) {
  response.pipe(file);
  file.on('finish', function() {
      file.close(cb);
  });
    });
}

 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us