Table data to CSV to Binary

  • Goal: I have a table with three columns of data: first name, last name, email address. I want to upload this data to Mailjet (an email service) using their REST API (docs here). The first step in their process is for me to POST to an endpoint with the data in the body of the post in CSV format, but binary encoded.

  • Steps:
    My first attempt was to create a Transformer that uses a js function to take the data from the table, convert it to a CSV formatted string, then binary encode it. Then I used the value of that Transformer in the API request. But every time I try this, Mailjet has errors importing the data. So maybe my js is wrong? Or maybe I'm making wrong assumptions about character encoding or something?
    Here is what I have now:

function jsonToCsvBinaryString(jsonArray) {
    if (!Array.isArray(jsonArray) || jsonArray.length === 0) {
        return '';
    }

    const keys = Object.keys(jsonArray[0]);
    const csvRows = [];

    // Add header row
    csvRows.push(keys.join(','));

    // Add data rows
    for (const obj of jsonArray) {
        const values = keys.map(key => {
            const escapedValue = ('' + obj[key]).replace(/"/g, '""'); // Escape double quotes
            return `"${escapedValue}"`;
        });
        csvRows.push(values.join(','));
    }

    // Convert CSV array to string
    const csvString = csvRows.join('\n');

    // Convert CSV string to binary string
    let binaryString = "";
    for (let i = 0; i < csvString.length; i++) {
        const binaryChar = csvString.charCodeAt(i).toString(2).padStart(8, '0');
        binaryString += binaryChar;
    }

    return binaryString;
}


const binaryString = jsonToCsvBinaryString({{adminTable.data}})
return binaryString

But more broadly, I'm wondering if there's a better way to do this. Can I somehow write the data from the table to a csv file "object" stored in memory somewhere in retool and then use that same object in the API request, treating it more like a file? And not having to use js to encode it into a binary string?

Or is there a better, more native way to generate the binary?

@jakedeg I'm currently working to implement an identical process (Mailgun instead of Mailjet). I haven't quite cracked it yet, but I'll watch this thread to see if someone is able to help.

If you come across a solution please let me know and I will do the same.

Hey! Just checking in to see if either found a solution or decided to use a different API?

If you're still working on this could you share any errors you're getting. @minijohn walks through the step on another post here: How to send email with mailgun sender from retool table - #2 by minijohn

"

  1. To actually send an email (mailgun or not) you'll need a few things (read more ):
  • From/To Address
  • Subject
  • Email body (html or not)
  1. I'm assuming your table holds just one of the 3 data points, the recipient's email address.
  • If that's the case you'll need some sort of process to specify the Subject and Body of the email
  1. On each row, you can add an action button that triggers a JS query for that row. Or a modal that lets you specify the subject/body for that record.
  • If this is a batch process, you'll need to slightly adapt this script to be able to take multiple selected rows and construct the final api calls that need to be made
  1. The actual REST Query that will send the data to mailgun.

"