Links

media

Media API Endpoint: Upload and manage your images and videos. Premium or Business Plan required.

Media API Endpoint

Manage your image and video gallery by uploading and retrieving images and videos.
Automatically resized Instagram images with the ideal dimensions of 1080 x 1080 px are provided. See the response example in the /media/upload below. Resized image names end in _1080x1080
Recommendation: If you already have your media accessible by an external URL, such as an S3 bucket, you can skip uploading the files to Ayrshare. Just POST to the /post endpoint with your externally accessible URL in the mediaURLs body parameter and your file will automatically be uploaded.
Click the in the endpoint to view details.
post
https://app.ayrshare.com/api
/media/upload
Upload Image or Video

Request Examples

cURL
Node JS
Python
PHP
curl \
-H "Authorization: Bearer API_KEY" \
-H 'Content-Type: application/json' \
-d '{"file": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...", "fileName": "test.png", "description": "best image"}' \
-X POST https://app.ayrshare.com/api/media/upload
const fetch = require("node-fetch");
const API_KEY = "API_KEY";
const base64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...";
fetch("https://app.ayrshare.com/api/media/upload", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
file: base64,
fileName: "test.png",
description: "best image"
}),
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
payload = {'file': 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...',
'fileName': "test.png",
'description': "best image"}
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY'}
r = requests.post('https://app.ayrshare.com/api/media/upload',
json=payload,
headers=headers)
print(r.json())
<?php
require 'vendor/autoload.php';// Composer auto-loader using Guzzle. See https://docs.guzzlephp.org/en/stable/overview.html
$client = new GuzzleHttp\Client();
$res = $client->request(
'POST',
'https://app.ayrshare.com/api/media/upload',
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer API_KEY'
],
'file' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...',
'fileName' => "test.png",
'description' => "best image"
]
);
echo json_encode(json_decode($res->getBody()), JSON_PRETTY_PRINT);
get
https://app.ayrshare.com/api
/media/uploadUrl
Upload Large Media Files

Request Examples

cURL
Node.js
Python
PHP
C#
curl \
-H "Authorization: Bearer [API Key]" \
-X GET https://app.ayrshare.com/api/media/uploadUrl?fileName=test.mov&contentType=mov
const fetch = require("node-fetch");
const API_KEY = "Your API Key";
fetch("https://app.ayrshare.com/api/media/uploadUrl?fileName=test.mov&contentType=mov", {
method: "GET",
headers: {
"Authorization": `Bearer ${API_KEY}`
}
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
headers = {'Authorization': 'Bearer [API_KEY]'}
r = requests.get('https://app.ayrshare.com/api/media/uploadUrl?fileName=test.mov&contentType=mov', headers=headers)
print(r.json())
<?php
require 'vendor/autoload.php'; // Composer auto-loader using Guzzle. See https://docs.guzzlephp.org/en/stable/overview.html
$client = new GuzzleHttp\Client();
$res = $client->request(
'GET',
'https://app.ayrshare.com/api/media/uploadUrl?fileName=test.mov&contentType=mov',
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer API_KEY'
]
]
);
echo json_encode(json_decode($res->getBody()), JSON_PRETTY_PRINT);
using System;
using System.Net;
using System.IO;
namespace MediaGETRequest_charp
{
class Media
{
static void Main(string[] args)
{
string API_KEY = "API_KEY";
string url = "https://app.ayrshare.com/api/media/uploadUrl?fileName=test.mov&contentType=mov";
var httpWebRequest = WebRequest.CreateHttp(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + API_KEY);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = streamReader.ReadToEnd();
Console.WriteLine(response);
}
}
}
}
  • accessUrl is the URL to access the media file after upload.
  • contentType is the content-type set for the media being upload. Use this in the Content-Type header when uploading the media.
  • uploadUrl is the URL used to PUT the media file. Please see below

Additional Endpoint Examples

The process to upload larger files:
  • Obtain an upload URL and access URL via the /media/uploadUrl endpoint. Please see above.
  • Upload the file via a PUT with Content-Type set to the returned contentType.
  • After uploading the media file, POST to the /post endpoint with the accessUrl in the mediaUrls body parameter.
  • The signed upload URL may only be uploaded to once. If you sent a bad file you must create a new upload URL.
curl -X PUT \
-H 'Content-Type: video/mp4' \
--upload-file ./test-video.mp4 uploadUrl
Please be sure that the contentType set when creating the uploadUrl matches the Content-Type and the file type when PUTing the file. For example, if you set the contentType to "image/png" when creating the uploadUrl, be sure to set the "Content-Type: image/png" and the uploaded file ends in ".png".

Example Upload File Binary in Node.js

Here is an example of uploading a binary media file using Node with JavaScript:
const fs = require("fs");
const request = require("request");
const API_KEY = "Your API Key";
const fileName = "test.png";
const endpoint = `https://app.ayrshare.com/api/media/uploadUrl?fileName=${fileName}&contentType=png`;
const run = async () => {
request.get(
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
url: endpoint,
},
(err, res, body) => {
if (err) {
return console.error(err);
}
const json = JSON.parse(body);
console.log("Upload URL:", json);
return fs.createReadStream(`./${fileName}`).pipe(
request.put(
json.uploadUrl,
{
headers: {
"Content-Type": json.contentType,
},
},
(err, httpsResponse, body) => {
if (err) {
console.error("err", err);
} else {
console.log(body);
}
}
)
);
}
);
};
run();

Example Upload Binary File in Postman

You may also use Postman to upload the binary file to the uploadUrl.
In Postman:
  1. 1.
    Select the HTTP Method PUT.
  2. 2.
    Paste your uploadUrl in the url field. Note, the url will expire after an hour and may only be used once. If you make a call and it fails, you must regenerate the uploadUrl.
  3. 3.
    In Headers set the Content-Type to be the content type returned in the /uploadUrl endpoint. For example: Content-Type: image/png.
  4. 4.
    Select Body -> binary and select the file to upload.
  5. 5.
    Press Send.
  6. 6.
    No return will occur, but you can check if the upload was successful by opening the accessUrl returned from the /uploadUrl endpoint in a browser.
Postman Binary Upload
Postman Binary Upload

Request Examples

cURL
Node JS
Python
PHP
C#
curl \
-H "Authorization: Bearer [API Key]" \
-X GET https://app.ayrshare.com/api/media
const fetch = require("node-fetch");
const API_KEY = "Your API Key";
fetch("https://app.ayrshare.com/api/media", {
method: "GET",
headers: {
"Authorization": `Bearer ${API_KEY}`
}
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
headers = {'Authorization': 'Bearer [API_KEY]'}
r = requests.get('https://app.ayrshare.com/api/media', headers=headers)
print(r.json())
<?php
require 'vendor/autoload.php'; // Composer auto-loader using Guzzle. See https://docs.guzzlephp.org/en/stable/overview.html
$client = new GuzzleHttp\Client();
$res = $client->request(
'GET',
'https://app.ayrshare.com/api/media',
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer API_KEY'
]
]
);
echo json_encode(json_decode($res->getBody()), JSON_PRETTY_PRINT);
using System;
using System.Net;
using System.IO;
namespace MediaGETRequest_charp
{
class Media
{
static void Main(string[] args)
{
string API_KEY = "API_KEY";
string url = "https://app.ayrshare.com/api/media";
var httpWebRequest = WebRequest.CreateHttp(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + API_KEY);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = streamReader.ReadToEnd();
Console.WriteLine(response);
}
}
}
}
Last modified 15d ago