URLs should be used when the image is available publicly on the internet.
/app.js
constHomey=require('homey');classAppextendsHomey.App {asynconInit() {constmyImage=awaitthis.homey.images.createImage();// the URL must start with https://myImage.setUrl("https://www.example.com/image.png"); }}module.exports= App;
Using a Stream
Streams should be used when downloading an image that cannot be supplied using Image#setUrl(). Using image streams involves writing data directly into a Node.js stream. Using streams requires homey version 2.2.0 or higher.
It is also possible to consume an image in your app, for instance through use of Flow Tokens.
const { PassThrough } =require("stream");constfetch=require("node-fetch");constFormData=require("form-data");//uploads an image to imgur and returns a linkasyncfunctionuploadImage(image) {conststream=awaitimage.getStream();constform=newFormData();form.append("image", stream, { contentType:stream.contentType, filename:stream.filename, name:"image", });form.append("description",`This image can also be (temporarily) viewed at: ${image.cloudUrl} and ${image.localUrl}` );constresponse=awaitfetch("https://api.imgur.com/3/image", { method:"POST", //pipe through a passthrough stream, workarround for a node-fetch bug involving form-data streams without content length set.
body:form.pipe(newPassThrough()), headers: {...form.getHeaders(), Authorization:"Client-ID <YOUR_CLIENT_ID>", }, });if (!response.ok) {thrownewError(response.statusText); }const { data } =awaitresponse.json();returndata.link;}