# Upload and Sharing Automation Example (upload-share.mjs)

This guide explains how to use a single script, `upload-share.mjs`, to upload local files and generate public share links using either the REST API or MCP (Model Context Protocol) mode.

## 1. Prerequisites
- **Runtime**: Node.js 22+ (native `fetch` and modern ESM support).
- **Account Plan**: Requires an active Pro, Business, or Lifetime developer account.
- **Required Scopes**: `uploads:write`, `files:read`, `shares:write`, `shares:read`.
- **Test Files**: Use non-sensitive local sample files (e.g., test PDF reports or sample clips) free of personal or confidential data.

## 2. Secure API Key Injection
Never hardcode your API key or pass it directly in shell command arguments where it could be logged in command history. Securely export it into your environment:

```bash
# Secure silent input (avoids shell history persistence)
read -s VIDEO2URL_API_KEY
export VIDEO2URL_API_KEY
```

## 3. Invocation Examples
The script requires three positional arguments: `mode` (`api` | `mcp`), file path, and MIME type.

```bash
# Mode 1: Upload PDF via REST API
node upload-share.mjs api ./report.pdf application/pdf

# Mode 2: Upload Video via MCP
node upload-share.mjs mcp ./clip.mp4 video/mp4
```

## 4. Technical Workflow and Safeguards
- **Precise Metadata**: The script obtains the exact physical file size in bytes via `openAsBlob` (`file.size`) as `sizeBytes`. The `mimeType` and `filename` must accurately match the media content.
- **Raw Binary PUT**: Stream the file directly to `upload.url` using a raw HTTP PUT request with all headers specified in `upload.headers`. **Do not include the API Bearer Token** in this storage PUT request.
- **Readiness Polling (Up to 5 Complete Rounds)**: After the PUT completes, invoke `complete_upload` (or the equivalent REST status workflow) for up to 5 rounds, checking status within each round until it becomes `ready` (the total number of status/round requests can exceed 5). If the status fails or does not become `ready`:
  - **Do not call create_share_link**.
  - **Do not blindly retry uploads** to avoid duplicate uploads and unintended quota consumption.
  - Unfinished or failed attempts may leave incomplete objects or upload records that should be inspected in your account dashboard.
- **Share Semantics**:
  - Generated links are `unlisted` (accessible to anyone with the URL) and created without a password; they do not implement private DRM or restricted entitlements.
  - **Feature Support Distinction**: While the API and MCP protocols inherently support password protection, this example script simply chooses not to specify one; custom expiration dates (`expiresAt`), however, are not supported by the underlying API. Use the web dashboard if you require custom expiration or password management.
- **Live Quota Usage & Manual Verification**: Running this script uploads real data, deducts from your active quota, and generates an accessible web link. The script outputs a JSON payload containing `url`, `fileId`, and `shareId`. Always manually open and verify the link in an incognito browser window.

## 5. Interface Configuration References
The following authentic screenshots provide guidance on dashboard configuration options (captured on 2026-10-16). They represent UI configuration states only and are not proof of execution results:
- [MCP Configuration Reference (en)](https://video2url.com/guides/screenshots/mcp-en.jpg)
- [REST API Configuration Reference (en)](https://video2url.com/guides/screenshots/api-en.jpg)
