File Upload using URL (for all file types)
When working with files like audio, images, videos, PDFs, or documents in your workflows, you often need to fetch them from a URL and upload them via an API. Since direct binary data isnβt always supported in APIs, viaSocket uses a standard approach: fetch β convert to base64 β send as JSON.
Why Base64?
APIs commonly accept base64-encoded files instead of raw binaries. This allows files to be:
Embedded in JSON payloads
Easily transmitted across APIs
Supported in actions like transcription, classification, extraction, and more
Supported File Types
This approach works for:
π§ Audio β MP3, WAV, OGG
πΌοΈ Images β PNG, JPG, GIF
π¬ Videos β MP4, MOV, AVI
π PDFs
π Documents β DOCX, XLSX, PPTX
π¦ Archives β ZIP, RAR
π§ Other binaries β firmware, installers
How It Works (in 4 steps)
1. Fetch File from URL
Use Axios to download the file in binary format:
jconst fileResponse = await axios.get(fileUrl, { responseType: 'arraybuffer' });
2. Convert File to Base64
Convert the binary data to base64:
const base64File = Buffer.from(fileResponse.data).toString('base64');
3. Prepare API Payload
Wrap the base64 in your request payload:
const payload = { data: base64File };
4. Send File to API
Send the payload to the API endpoint:
const apiResponse = await axios.post('https://api.example.com/v1/upload', payload, { headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json', }, });
Use Case: Audio Upload for Transcription (ChatGPT)
const audioUrl = context?.inputData?.url; const audioResponse = await axios.get(audioUrl, { responseType: 'arraybuffer' }); const base64str = Buffer.from(audioResponse.data).toString('base64'); const payload = { model: 'gpt-4o-audio-preview', modalities: ['text'], messages: [ { role: 'user', content: [ { type: 'text', text: 'Please transcribe this audio recording.' }, { type: 'input_audio', input_audio: { data: base64str, format: 'mp3' } } ] } ], store: true, }; const apiResponse = await axios.post('https://api.openai.com/v1/chat/completions', payload, { headers: { 'Content-Type': 'application/json' }, }); return { Result: apiResponse.data.choices[0].message.content, ID: apiResponse.data.id, Created: new Date(apiResponse.data.created * 1000).toLocaleString(), Model: apiResponse.data.model, };
Optional: Filter by Execution Time (for Scheduled Triggers)
To upload only recent files, you can filter data based on when the trigger was executed:
const minutesAgo = new Date(__executionStartTime__ - context?.inputData?.scheduledTime * 60 * 1000);
This calculates the time X minutes before the trigger ran.