JavaScript example

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
JavaScript uploadFormDataserver routeworkerfetch
Example
Language
JavaScript
Input
invoice.pdf
Output
url and file_id
Short answer

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
Handoff

One request in, a URL and JSON out

Request
const form = new FormData();
form.append("file", file);
form.append("visibility", "public");
Response
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 };
Runtime

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.

01

Backend route

Accept a user upload, call GetFileURL server-side, then return only the fields the UI needs.

02

Worker

Use platform secrets and stream or forward files from an edge function when that matches your architecture.

03

Job runner

Upload generated files from background jobs before passing the returned URL to downstream APIs.

Handling

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.

01

url

Send this field to OCR, AI, social, document, CRM, or webhook APIs that fetch files by URL.

02

file_id

Store this field for delete calls and support debugging.

03

expires_at

Use the timestamp to prevent late destination fetches from receiving expired URLs.

Examples

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 };
FAQ

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.