How to Modify a Pipedream Workflow Code to Function in a Cloud Convert Node.js Instance?

This topic was automatically generated from Slack. You can find the original thread here.

Could you help me modify this code to work in a Cloud Convert node.js instance?

// Assuming you're in a Pipedream workflow

export default defineComponent({
  async run({ steps, $ }) {
    // Construct the JSON payload dynamically
    const payload = {
      "tasks": {
        "import-my-file": {
          "operation": "import/url",
          "url": steps.trigger.event.webContentLink // Correctly insert the variable
        },
        "convert-my-file": {
          "operation": "convert",
          "input": "import-my-file",
          "input_format": "mp4",
          "output_format": "mp3"
        },
        "export-my-file": {
          "operation": "export/url",
          "input": "convert-my-file"
        }
      },
      "redirect": true
    };

    // Use axios to send a POST request to CloudConvert with the payload
    const response = await axios($, {
      method: "POST",
      url: `https://api.cloudconvert.com/v2/jobs`,
      headers: {
        Authorization: `Bearer YOUR_CLOUDCONVERT_API_KEY`,
      },
      data: payload
    });

    // Handle the response
    // ...

    return response.data; // or handle as needed
  }
});

Sure, I can help you modify the code to work in a Cloud Convert Node.js instance. However, I need to clarify a few things: 1. Are you looking to run this code within a Pipedream workflow or outside of Pipedream? 2. If you’re looking to run this outside of Pipedream, please note that the axios function used in the code is a special version provided by Pipedream. If you’re running this code outside of Pipedream, you’ll need to install the axios package and use it instead. 3. You’ll also need to replace steps.trigger.event.webContentLink with the actual URL you want to convert. 4. Lastly, replace YOUR_CLOUDCONVERT_API_KEY with your actual CloudConvert API key. Please provide more details so I can assist you more accurately.

I want to run it inside pipedream

The code you’ve provided is already set up to run inside a Pipedream workflow. However, you need to make sure of a few things: 1. The steps.trigger.event.webContentLink is correctly providing the URL of the file you want to convert. This assumes that the trigger of your workflow is providing an event with a webContentLink property. If not, you’ll need to replace this with the actual URL of the file you want to convert. 2. Replace YOUR_CLOUDCONVERT_API_KEY with your actual CloudConvert API key. 3. The axios function used in the code is a special version provided by Pipedream. It’s already available in the Pipedream environment, so you don’t need to import it. 4. If you want to handle the response in a specific way, you can add your code in the // Handle the response section. 5. The return response.data; line will return the data from the CloudConvert API as the output of this step in the workflow. You can change this to handle the response as needed. If you encounter any issues or errors, please provide more details so I can assist you further.

I keep receiving this error:

ReferenceError
axios is not defined

DETAILS
    at Object.run (file:///tmp/__pdg__/dist/code/32567e4f7b3669d0348a054dce185c7f4ee170b89fea765adebabadd77d4a96b/component.mjs:23:22)
    at null.executeComponent (/var/task/launch_worker.js:267:53)
    at MessagePort.messageHandler (/var/task/launch_worker.js:764:28)

I keep receiving this error:

ReferenceError
axios is not defined

DETAILS
    at Object.run (file:///tmp/__pdg__/dist/code/32567e4f7b3669d0348a054dce185c7f4ee170b89fea765adebabadd77d4a96b/component.mjs:23:22)
    at null.executeComponent (/var/task/launch_worker.js:267:53)
    at MessagePort.messageHandler (/var/task/launch_worker.js:764:28)

I see that you’re getting a ReferenceError saying axios is not defined. This error is occurring because the axios function is not being imported correctly in your Pipedream component. In Pipedream, you should import axios from the @pipedream/platform package, not the standard axios package. Here’s how you should do it: import { axios } from "@pipedream/platform"; Then, you can use axios in your run method like this: const response = await axios($, { method: "POST", url: `https://api.cloudconvert.com/v2/jobs`, headers: { Authorization: `Bearer YOUR_CLOUDCONVERT_API_KEY`, }, data: payload }); Please replace your axios usage in your code with this and try again. If you encounter any further issues, please let me know!

Import axios from pipedream/plateform.
And double check the ‘Bearer KEY’ should be something like Bearer ${KEY}

rewrite the full code

Sure, here is the revised code with the correct import statement for axios and the correct format for the Bearer token: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ $ }) { const payload = { tasks: [ { operation: "import/url", input: { url: steps.trigger.event.webContentLink, }, }, { operation: "convert", input_format: "pdf", output_format: "png", engine: "office", }, { operation: "export/url", input: "@parent.tasks.convert.output", }, ], }; const response = await axios($, { method: "POST", url: "https://api.cloudconvert.com/v2/jobs", headers: { Authorization: `Bearer ${process.env.YOUR_CLOUDCONVERT_API_KEY}`, }, data: payload, }); // Handle the response console.log(response.data); return response.data; }, }); Please replace YOUR_CLOUDCONVERT_API_KEY with your actual CloudConvert API key.