This post covers how to convert a base64-encoded string and upload it as a PDF directly into Google Drive. The approach uses the Zapier SDK inside a Code by Zapier step, so your existing Google Drive app connection handles authentication automatically, no API keys are needed in your code, and no additional action steps are required to upload the file after decoding.
Step-by-step guide
- Add a Code by Zapier step. In your existing Zap, click the + icon to add a new action step and search for Code by Zapier. Select Run JavaScript as the action event, then click Open in Code Editor.
- Enable the Zapier SDK. In the Code Editor, click the Packages icon in the left sidebar and toggle on @zapier/zapier-sdk.
- Add your app connection. Then click the App Connections icon in the left sidebar. From there, click + Add Connection, select Google Drive, and choose your connected account.
- Add the input data. In the Input Data section, add the following three key/value pairs, mapping each value from your trigger data:
- fileContent - select the field containing the base64-encoded PDF string from the trigger or action step that provides it.
- fileName - enter the filename to use in Google Drive. To have the filename generated dynamically, select the relevant fields from the trigger or other action steps.
- folderId - the Google Drive folder ID where the file should be saved (you can find this in the folder's URL). If the folder needs to be saved to a different folder every time use a Find a Folder search action to have the ID dynamically populated.
- Add the code. Use Copilot or your preferred AI agent to generate the necessary code, or add the following example code into the Code editor:
import { createZapierSdk } from '@zapier/zapier-sdk';
const zapier = createZapierSdk();
/**
* @param {{inputData: InputData}}
*/
export default async function main({inputData}) {
const fileContent = inputData.fileContent || '';
const fileName = inputData.fileName || 'document.pdf';
const folderId = inputData.folderId || '';
if (!fileContent) {
throw new Error('base64String is required');
}
// Decode base64 to binary buffer
const fileBuffer = Buffer.from(fileContent, 'base64');
// Generate a unique boundary for multipart form data
const boundary = `----WebKitFormBoundary${Math.random().toString(36).substring(2, 15)}`;
// Build the multipart body with metadata and file content
const parts = [];
// Add metadata part (file name and parent folder)
const metadataPart = `--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${JSON.stringify({
name: fileName,
parents: [folderId],
})}\r\n`;
parts.push(Buffer.from(metadataPart));
// Add file part with proper headers and binary data
const filePart = `--${boundary}\r\nContent-Type: application/pdf\r\nContent-Transfer-Encoding: binary\r\n\r\n`;
parts.push(Buffer.from(filePart));
parts.push(fileBuffer);
// Add closing boundary
const closingBoundary = `\r\n--${boundary}--\r\n`;
parts.push(Buffer.from(closingBoundary));
// Concatenate all parts into a single buffer
const multipartBody = Buffer.concat(parts);
// Upload to Google Drive using the multipart upload endpoint
const response = await zapier.fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name,webViewLink', {
method: 'POST',
connectionId: connections.google_drive,
headers: {
'Content-Type': `multipart/related; boundary=${boundary}`,
},
body: multipartBody,
});
if (!response.ok) {
throw new Error(`Failed to upload file: HTTP ${response.status} ${response.statusText}`);
}
const data = await response.json();
return {
fileId: data.id,
fileName: data.name,
webViewLink: data.webViewLink,
};
}
- Test the Code step. Click Run Code and check the Output Data panel. A successful run returns a File Id, File Name and Web View Link field, confirming the PDF was uploaded to your Drive folder. If you need to pass the uploaded file to downstream steps, see the FAQ below.
Notes and limitations
- The Zapier SDK is available on all paid Zapier plans, and as well as the Free plan for a limited time.
- Zapier SDK handles Google Drive authentication securely through your existing Zapier connection. No API keys or tokens go in your code or Zap history.
FAQs
Q: Do I need to know how to code to use this?
A: No. You can copy the code exactly as written into the Code editor, no modifications are needed as long as you set up the three Input Data fields as outlined in the steps above (fileContent, fileName, folderId). The only thing to adjust is the values you map to those fields.
Q: How do I find my Google Drive folder ID?
A: Open the folder in Google Drive in your browser. The folder ID is the string of characters at the end of the URL — for example, in drive.google.com/drive/folders/1ABC123xyz, the folder ID is 1ABC123xyz. If you leave the folderId input blank, the file will be saved to the root of My Drive.
Q: Can I pass the uploaded PDF file to other steps?
A: It depends on what your next step needs. If the next action is a Google Drive action then you could pass it the file’s ID from the Code step directly. If the action needs a file object shown as "(Exists but not shown)" or a publicly accessible URL then you’d need the Retrieve File or Folder by ID step to produce the file object or get it’s URL first.
Q: What if my base64 string arrives inside a JSON payload rather than as a plain field?
A: If your webhook delivers the base64 string nested inside a JSON object, Zapier will flatten the nested fields using double-underscore notation. Map the correct flattened field to the fileContent input for example, if the payload is {"data": {"file": "..."}}, the field will appear as data__file in your trigger output.
Q: Does it only work with Google Drive or can it upload it elsewhere?
A: You can use this same approach to upload the file to other file hosting platforms, just swap out the Google Drive specific parts to reference the other app’s API instead. If you’re not comfortable with coding you can use the Generate with AI feature to have Copilot adjust the code for you.
Q: How many tasks will it use to run this Code step?
A: It depends on what the code does and how long it runs, which can vary with the size of the file being processed. See How is Code by Zapier usage measured for details. For very large PDFs, you may also need to configure extended runtimes to avoid timeouts.

