Frequently you have business logic interactions with third-party services that return binary files as base64 encoded strings. Afterwards you need to upload this base64 files as a Kinvey file. The following Flex service code shows how to do that. It reads a PDF from an external service but this code can be used to read a PNG, JPG or other binary format and upload it. Do not forget to change the mimeType, for instance, to "image/png" if you are reading an image. 


You can see the code in index.js which can read a PDF from external source as base64 string and upload the file to Kinvey. This way you will be able to receive the file in base64 from your service and after uploading it in Kinvey, associate it with the relevant data entity.


The sample relies on the Kinvey Node SDK (to work with the backend data), the request module to make external HTTP requests and the Kinvey Flex SDK which exposes the interfaces and modules to work with your app's backend from Flex. The dependencies are shown in the package.json. 


The sample is using the app's Master Secret - this gives you unlimited privileges to the API. Do not deploy the master key to a public place. Keep the master secret in a secure place. 


You can test the flex file locally as explained in an article at the following URL.


https://devcenter.kinvey.com/rest/guides/flex-services#testing-locally 


Start the Node.js application and make a POST request to localhost:10001/_flexFunctions/fileUpload with an empty body and specifying the required headers  (in this function you will only need to specify the shared secret and backend keys. For example: 


POST /_flexFunctions/fileUpload HTTP/1.1

Host: localhost:10001

Content-Type: application/json

X-Auth-Key: my-actual-shared-secret

X-Kinvey-App-Metadata: {"_id": "kid_......", "appsecret": "42......", "mastersecret": "28.......", "baasUrl": "https://baas.kinvey.com"}


{

 

}


Addiitonal resources:


Kinvey FLEX CLI and runtime information - https://devcenter.kinvey.com/rest/guides/flexservice-runtime 


Developing your Flex function- https://devcenter.kinvey.com/rest/guides/flex-services#RegisteringFlexFunctionHandlers