Skip to main content
Upload a local video, wait for Gemini file processing, and reference the resulting URI in a native generateContent request.
This file workflow is only available for Google Gemini models that support native fileData input. Uploaded file URIs cannot be used with OpenAI, Anthropic, /chat/completions, /messages, image-generation, or video-generation endpoints.

Before you begin

You need:
  • A LinkModel API key with access to a compatible Gemini model, such as gemini-3.1-flash-lite.
  • A local video file. The examples below use sample.mp4 with MIME type video/mp4.
  • curl and jq.
Set your API key without placing it directly in shell history:
The file lifecycle uses https://api.linkmodel.ai/v1. The Gemini-native request uses https://api.linkmodel.ai/v1beta.
Use a portable ASCII value for UPLOAD_FILENAME, containing only letters, numbers, dots, hyphens, or underscores. The local path may contain spaces or non-ASCII characters, but the upload filename becomes part of the Gemini file URI and should not.

1. Calculate file metadata

The upload session requires the exact file size and a hexadecimal MD5 checksum.

2. Create a Gemini upload session

A successful response contains a file identifier and a short-lived upload instruction:
Extract the values needed by the next steps:
md5sum in the session request is hexadecimal. Any Content-MD5 upload header is the Base64 value returned by the server. Do not convert or interchange them. Header names are case-insensitive, so avoid extracting them by an assumed capitalization.

3. Upload the video bytes

Send the file directly to the returned pre-signed URL. Use the returned upload headers and values unchanged.
The object-storage response is normally HTTP 200 with an empty or very small body. The pre-signed URL and its headers are credentials for this upload; do not log or share them.

4. Complete the upload

After the direct upload succeeds, notify LinkModel so processing can begin:
An HTTP 202 response means processing has started. It does not mean the file is ready for Gemini yet.

5. Wait until the file is active

Poll the file resource until it becomes active or reaches the timeout:
The active resource includes a Gemini file URI:
Keep the gs:// URI unchanged. It is an account-scoped Gemini reference, not a public download URL.

6. Analyze the video with Gemini

Reference the active file URI using fileData.fileUri and include the matching MIME type:
A successful response uses Gemini’s native response format rather than LinkModel’s code, data, and msg business envelope:
Gateway errors use LinkModel’s error envelope instead. Save request_id when contacting support:

7. Delete the file

Delete the uploaded file when you no longer need it:

Complete runnable example

The following script runs the complete lifecycle and attempts to delete the remote file even when a later step fails. It uses gemini-3.1-flash-lite and keeps temporary response files outside your project directory.

Troubleshooting

API Reference