Fetch a file from a URL
Short answer
Fetch a file from a URL with POST /v2/files/fetch. This can be any URL, either public or private (via HTTP authentication). In the url parameter, enter the full URL of the remote file. Each call needs a bearer token and counts towards your hourly API limit.
After reading this you will know
- How to fetch a file from a URL
- 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.
Fetch URL
Section titled “Fetch URL”https://api.sirv.com/v2/files/fetch
Use this API method to fetch a file(s) from a URL via HTTP and save it to your Sirv account.
This can be any URL, either public or private (via HTTP authentication). In the url parameter, enter the full URL of the remote file.
In the filename parameter, enter the folder path and filename where it should be saved e.g. /path/to/folder/new-filename.jpg.
If the folder path does not already exist in your Sirv account new folder(s) will be created.
Example request
// Your bearer token, from POST /v2/token, in the SIRV_TOKEN environment variableconst res = await fetch('https://api.sirv.com/v2/files/fetch', { method: 'POST', headers: { authorization: `Bearer ${process.env.SIRV_TOKEN}`, 'content-type': 'application/json' }, body: JSON.stringify([ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" } ]),});console.log(res.status, await res.text());import requests
res = requests.post( 'https://api.sirv.com/v2/files/fetch', headers={'authorization': 'Bearer BEARER_TOKEN', 'content-type': 'application/json'}, json=[ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" } ],)print(res.status_code, res.text)curl --request POST \ --url 'https://api.sirv.com/v2/files/fetch' \ --header 'authorization: Bearer BEARER_TOKEN' \ --header 'content-type: application/json' \ --data '[{"url":"https://demo.sirv.com/aurora.jpg","filename":"/REST API Examples/aurora.jpg"},{"url":"https://demo.sirv.com/missing.jpg","filename":"/REST API Examples/missing.jpg"}]'import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;
HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.sirv.com/v2/files/fetch")) .header("authorization", "Bearer BEARER_TOKEN") .header("content-type", "application/json") .method("POST", HttpRequest.BodyPublishers.ofString(""" [ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.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/fetch");request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "BEARER_TOKEN");request.Content = new StringContent("""[ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" }]""", System.Text.Encoding.UTF8, "application/json");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/fetch');curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => ['authorization: Bearer BEARER_TOKEN', 'content-type: application/json'], CURLOPT_POSTFIELDS => json_encode([ [ 'url' => 'https://demo.sirv.com/aurora.jpg', 'filename' => '/REST API Examples/aurora.jpg' ], [ 'url' => 'https://demo.sirv.com/missing.jpg', 'filename' => '/REST API Examples/missing.jpg' ] ]), CURLOPT_RETURNTRANSFER => true,]);echo curl_exec($ch);package main
import ( "fmt" "io" "net/http" "strings")
func main() { body := strings.NewReader(`[ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" } ]`) req, _ := http.NewRequest("POST", "https://api.sirv.com/v2/files/fetch", body) req.Header.Set("authorization", "Bearer BEARER_TOKEN") req.Header.Set("content-type", "application/json") 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/fetch')req = Net::HTTP::Post.new(uri)req['authorization'] = 'Bearer BEARER_TOKEN'req['content-type'] = 'application/json'req.body = <<~'JSON' [ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" } ]JSONres = 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/fetch")!)request.httpMethod = "POST"request.setValue("Bearer BEARER_TOKEN", forHTTPHeaderField: "authorization")request.setValue("application/json", forHTTPHeaderField: "content-type")request.httpBody = #"""[ { "url": "https://demo.sirv.com/aurora.jpg", "filename": "/REST API Examples/aurora.jpg" }, { "url": "https://demo.sirv.com/missing.jpg", "filename": "/REST API Examples/missing.jpg" }]"""#.data(using: .utf8)let (data, response) = try await URLSession.shared.data(for: request)print((response as! HTTPURLResponse).statusCode, String(decoding: data, as: UTF8.self))Example response
[ { "filename": "/REST API Examples/aurora.jpg", "success": true, "attempted": false, "attempts": [ { "url": "https://demo.sirv.com/aurora.jpg", "initiator": { "type": "api", "remoteAddr": "176.105.166.4", "date": "2020-07-17T15:47:35.316Z" }, "statusCode": 200, "headers": { "result": { "version": "HTTP/1.1", "code": 200, "reason": "OK" }, "Date": "Fri, 17 Jul 2020 15:47:36 GMT", "Content-Type": "image/webp", "Content-Length": "201846", "Connection": "keep-alive", "Last-Modified": "Fri, 17 Jul 2020 15:47:36 GMT", "ETag": "\"5f11c818-31476\"", "Server": "Sirv.Imagination", "X-Sirv-Server": "c1-extra1-fireball-13", "X-Sirv-Cache": "MISS", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*", "Expires": "Sun, 16 Aug 2020 15:47:36 GMT", "Cache-Control": "max-age=2592000", "X-Sirv-Meta-Width": "2500", "X-Sirv-Meta-Height": "1667", "X-Sirv-Shard": "c1-riak1", "X-Account-Id": "sdulth0oi0t9zxpxqtxwkwvgipjgv6ud", "X-File-VersionId": "5evxYG8lnBgeA7cGBRTZzq7JQolRYviN:0", "X-Account-Serial": "2020-06-15T19:18:53.901Z", "Accept-Ranges": "bytes" }, "timing": { "ns": 0.007514, "connect": 0.007912, "start": 0.48724, "total": 0.489622 }, "date": "2020-07-17T15:47:36.712Z" } ], "retryAfter": "2020-07-18T15:47:36.712Z" }, { "filename": "/REST API Examples/missing.jpg", "success": false, "attempted": false, "attempts": [ { "url": "https://demo.sirv.com/missing.jpg", "initiator": { "type": "api", "remoteAddr": "176.105.166.4", "date": "2020-07-18T08:35:51.026Z" }, "statusCode": 404, "headers": { "result": { "version": "HTTP/1.1", "code": 404, "reason": "Not Found" }, "Date": "Sat, 18 Jul 2020 08:35:52 GMT", "Content-Type": "text/html; charset=utf-8", "Content-Length": "4140", "Connection": "keep-alive", "Vary": "Accept-Encoding", "X-Account-Id": "sdulth0oi0t9zxpxqtxwkwvgipjgv6ud", "X-Account-Serial": "2020-06-15T19:18:53.901Z", "ETag": "W/\"102c-wKpRt+Cg9Cnw/NpTSkF5+w\"", "X-Sirv-Cache": "MISS", "Server": "Sirv.Imagination", "X-Sirv-Server": "c1-extra1-fireball-15", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*" }, "timing": { "ns": 0.018986, "connect": 0.019303, "start": 0.103774, "total": 0.103829 }, "error": { "name": "Error", "message": "Request not successful: expected status code 200 but got 404" }, "date": "2020-07-18T08:35:52.089Z" } ], "retryAfter": "2020-07-18T12:07:32.613Z" }]