Upload a file
Short answer
Upload a file with POST /v2/files/upload. If the folder path does not already exist, it will be created. Specify the appropriate content type. Use binary content encoding (not base64). Each call needs a bearer token and counts towards your hourly API limit.
After reading this you will know
- How to upload a file
- Which parameters it takes
- Sample code in 9 languages and a sample answer
This command needs a bearer token and counts towards your hourly limit.
Upload file
Section titled “Upload file”https://api.sirv.com/v2/files/upload
Use this API method to upload a file. If the folder path does not already exist, it will be created. Specify the appropriate content type.
Use binary content encoding (not base64).
Query parameters
| Name | Type | Required | Example | Description |
|---|---|---|---|---|
filename | string | Yes | /REST API Examples/uploaded.txt | Path to save the file at, with its name. Missing folders are created. |
Example request
import { readFile } from 'node:fs/promises';
// Your bearer token, from POST /v2/token, in the SIRV_TOKEN environment variableconst res = await fetch('https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt', { method: 'POST', headers: { authorization: `Bearer ${process.env.SIRV_TOKEN}`, 'content-type': 'image/jpeg' }, body: await readFile('image.jpg'),});console.log(res.status, await res.text());import requests
res = requests.post( 'https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt', headers={'authorization': 'Bearer BEARER_TOKEN', 'content-type': 'image/jpeg'}, data=open('image.jpg', 'rb'),)print(res.status_code, res.text)curl --request POST \ --url 'https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt' \ --header 'authorization: Bearer BEARER_TOKEN' \ --header 'content-type: image/jpeg' \ --data-binary @image.jpgimport java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.nio.file.Path;
HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt")) .header("authorization", "Bearer BEARER_TOKEN") .header("content-type", "image/jpeg") .method("POST", HttpRequest.BodyPublishers.ofFile(Path.of("image.jpg"))) .build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());System.out.println(response.statusCode() + " " + response.body());using System.Net.Http.Headers;
using var client = new HttpClient();var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt");request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "BEARER_TOKEN");request.Content = new ByteArrayContent(File.ReadAllBytes("image.jpg"));request.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");var response = await client.SendAsync(request);Console.WriteLine($"{(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}");<?php$ch = curl_init('https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt');curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => ['authorization: Bearer BEARER_TOKEN', 'content-type: image/jpeg'], CURLOPT_POSTFIELDS => file_get_contents('image.jpg'), CURLOPT_RETURNTRANSFER => true,]);echo curl_exec($ch);package main
import ( "fmt" "io" "net/http" "os")
func main() { body, _ := os.Open("image.jpg") defer body.Close() req, _ := http.NewRequest("POST", "https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt", body) req.Header.Set("authorization", "Bearer BEARER_TOKEN") req.Header.Set("content-type", "image/jpeg") res, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer res.Body.Close() out, _ := io.ReadAll(res.Body) fmt.Println(res.Status, string(out))}require 'net/http'
uri = URI('https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt')req = Net::HTTP::Post.new(uri)req['authorization'] = 'Bearer BEARER_TOKEN'req['content-type'] = 'image/jpeg'req.body = File.binread('image.jpg')res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }puts res.code, res.bodyimport Foundation
var request = URLRequest(url: URL(string: "https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt")!)request.httpMethod = "POST"request.setValue("Bearer BEARER_TOKEN", forHTTPHeaderField: "authorization")request.setValue("image/jpeg", forHTTPHeaderField: "content-type")request.httpBody = try Data(contentsOf: URL(fileURLWithPath: "image.jpg"))let (data, response) = try await URLSession.shared.data(for: request)print((response as! HTTPURLResponse).statusCode, String(decoding: data, as: UTF8.self))Response
HTTP 200 with an empty body when the command succeeds.