CSV to Base64 for attachments and APIs
Sending a CSV file through an API usually means turning it into a string first. The CSV to Base64 tool encodes your CSV — pasted or opened as a file — as Base64, keeping quoting and delimiters exactly as they are. An opened file is encoded from its own bytes, so the receiver gets a byte-identical file; pasted text is encoded as UTF-8 with LF line breaks.
Example
Input:
id,name,city
1,Ada,London
2,Linus,HelsinkiOutput:
aWQsbmFtZSxjaXR5CjEsQWRhLExvbmRvbgoyLExpbnVzLEhlbHNpbmtp
Where you need Base64 CSV
- Email APIs. Transactional email services such as SendGrid, Postmark or Amazon SES (raw messages) expect attachment content as Base64, together with a filename and a MIME type such as
text/csv. - JSON-based import endpoints that accept a file as a string field instead of a multipart upload.
- Download links without a server. A link with
href="data:text/csv;base64,…"and adownload="report.csv"attribute lets a browser save generated CSV directly. - Test fixtures that embed a sample file in a JSON or YAML test case.
Because the Base64 value contains no commas, quotes or line breaks, it can sit safely inside a JSON string, an XML element or a form field without any further escaping — which is the whole point of encoding a CSV file this way.
A client-side download link
const csv = "id,name\n1,Ada";
const b64 = btoa(csv); // ASCII only; encode UTF-8 first for other text
link.href = "data:text/csv;base64," + b64;
link.download = "report.csv";For larger files, a Blob with URL.createObjectURL() is more efficient than a data URI, because the browser does not have to hold a long Base64 string in the page.
Excel and character encoding
The CSV is encoded as UTF-8. Excel on Windows sometimes opens UTF-8 CSV files as Windows-1252 and shows “é” instead of “é”, unless the file begins with a UTF-8 byte order mark. If your recipients use Excel and the data contains accented characters, add the BOM when generating the file, or ask them to import it via Data › From Text/CSV and choose UTF-8.
Line endings
RFC 4180 specifies CRLF line endings for CSV, while most tools accept plain LF as well. A CSV opened as a file keeps its own line endings, so the decoded file matches it exactly. Text you type or paste is encoded with LF line breaks, because the editor stores every line break as a line feed. If a strict importer needs CRLF, open the CSV as a file instead of pasting it.
Decode Base64 CSV with Base64 to CSV, or turn the data into JSON with CSV to JSON. Encoding happens in your browser; your data is not uploaded.