JavaScript example for file URL uploads
Your app has the file and the next job needs a URL. Run this request from a trusted server, worker, or backend route.
- Reviewed by
- GetFileURL technical team
- Updated
- Language
- JavaScript
- Input
- invoice.pdf
- Output
- url and file_id
What this page answers
It returns JSON fields including the direct public URL, file ID, content type, and expiry metadata when a retention window is set.
- Reviewed by
- GetFileURL technical team
- Last updated
One request in, a URL and JSON out
const form = new FormData();
form.append("file", file);
form.append("visibility", "public");const res = await fetch("https://api.getfileurl.com/v1/files", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.GETFILEURL_KEY}` },
body: form,
});
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
const upload = await res.json();
return { url: upload.url, fileId: upload.file_id };Run the request where the API key is protected.
Server-side JavaScript can enforce account policy and avoid exposing a bearer key in browser bundles.
Backend route
Accept a user upload, call GetFileURL server-side, then return only the fields the UI needs.
Worker
Use platform secrets and stream or forward files from an edge function when that matches your architecture.
Job runner
Upload generated files from background jobs before passing the returned URL to downstream APIs.
Keep the response shape explicit in code.
Downstream code should not parse a CDN URL to learn file identity. Use the JSON fields returned by the upload response.
url
Send this field to OCR, AI, social, document, CRM, or webhook APIs that fetch files by URL.
file_id
Store this field for delete calls and support debugging.
expires_at
Use the timestamp to prevent late destination fetches from receiving expired URLs.
Copy the same upload shape into code
const form = new FormData();
form.append("file", file);
form.append("visibility", "public");
const res = await fetch("https://api.getfileurl.com/v1/files", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.GETFILEURL_KEY}` },
body: form,
});
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
const upload = await res.json();
return { url: upload.url, fileId: upload.file_id };Common questions
What does the JavaScript example return?
It returns JSON fields including the direct public URL, file ID, content type, and expiry metadata when a retention window is set.
Where should I store the API key?
Store the key in a server environment variable, worker secret, script secret manager, or trusted automation credential store.
What should I do after getting the URL?
Pass `url` to the next API and store `file_id` if the workflow should delete or inspect the file later.