# Send as Multipart Form-Data
curl \
-H "Authorization: Bearer API_KEY" \
-F "file=@test.png" \
-F "fileName=test.png" \
-F "description=best image" \
-X POST https://api.ayrshare.com/api/media/upload
# Send as Base64
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://api.ayrshare.com/api/media/upload
// Send as Multipart Form-Data
const FormData = require('form-data');
const fs = require('fs');
const API_KEY = "API_KEY";
const imagePath = './test.png';
const form = new FormData();
form.append('file', fs.createReadStream(imagePath));
form.append('fileName', 'test.png');
form.append('description', 'best image');
fetch("https://api.ayrshare.com/api/media/upload", {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`
// Don't set Content-Type header - FormData will set it automatically with boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Send as Base64
const API_KEY = "API_KEY";
const base64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...";
fetch("https://api.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);
# Send as Multipart Form-Data
import requests
# For a local file:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Form data
data = {
'fileName': 'test.png',
'description': 'best image'
}
headers = {
'Authorization': 'Bearer API_KEY'
}
r = requests.post(
'https://api.ayrshare.com/api/media/upload',
files=files,
data=data,
headers=headers
)
print(r.json())
# Send as Base64
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://api.ayrshare.com/api/media/upload',
json=payload,
headers=headers)
print(r.json())
# Send as Multipart Form-Data
<?php
// Generate a boundary string
$boundary = uniqid();
// Open the file
$file = file_get_contents('test.png');
// Build the multipart form data
$data = '';
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="file"; filename="test.png"' . "\r\n";
$data .= "Content-Type: image/png\r\n\r\n";
$data .= $file . "\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="fileName"' . "\r\n\r\n";
$data .= "test.png\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="description"' . "\r\n\r\n";
$data .= "best image\r\n";
$data .= "--" . $boundary . "--\r\n";
// Setup the context for the request
$options = [
'http' => [
'method' => 'POST',
'header' => "Authorization: Bearer API_KEY\r\n" .
"Content-Type: multipart/form-data; boundary=" . $boundary . "\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
]
];
// Send the request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// Print the response
echo $result;
# Send as Base64
<?php
$data = [
'file' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...',
'fileName' => "test.png",
'description' => "best image"
];
$ch = curl_init('https://api.ayrshare.com/api/media/upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer API_KEY'
]);
$response = curl_exec($ch);
curl_close($ch);
echo json_encode(json_decode($response), JSON_PRETTY_PRINT);
{
"id": "1167335b-6c37-4fc6-ab8a-044e0005d335-jpeg",
"url": "https://images.ayrshare.com/q3Ls85VTsrbODnGIJHpy7PaHWwA3/1167335b-6c37-4fc6-ab8a-044ed885d.jpeg",
"fileName": "fun.jpg",
"description": "good times"
}
{
"action": "request",
"status": "error",
"code": 101,
"message": "Missing or incorrect parameters. Please verify with the docs. .../ayrshare.com/rest-api/endpoints"
}
Media
Upload de imagem ou vídeo
Faça upload de uma imagem ou de um pequeno arquivo de vídeo para incluir na sua publicação
POST
/
media
/
upload
# Send as Multipart Form-Data
curl \
-H "Authorization: Bearer API_KEY" \
-F "file=@test.png" \
-F "fileName=test.png" \
-F "description=best image" \
-X POST https://api.ayrshare.com/api/media/upload
# Send as Base64
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://api.ayrshare.com/api/media/upload
// Send as Multipart Form-Data
const FormData = require('form-data');
const fs = require('fs');
const API_KEY = "API_KEY";
const imagePath = './test.png';
const form = new FormData();
form.append('file', fs.createReadStream(imagePath));
form.append('fileName', 'test.png');
form.append('description', 'best image');
fetch("https://api.ayrshare.com/api/media/upload", {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`
// Don't set Content-Type header - FormData will set it automatically with boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Send as Base64
const API_KEY = "API_KEY";
const base64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...";
fetch("https://api.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);
# Send as Multipart Form-Data
import requests
# For a local file:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Form data
data = {
'fileName': 'test.png',
'description': 'best image'
}
headers = {
'Authorization': 'Bearer API_KEY'
}
r = requests.post(
'https://api.ayrshare.com/api/media/upload',
files=files,
data=data,
headers=headers
)
print(r.json())
# Send as Base64
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://api.ayrshare.com/api/media/upload',
json=payload,
headers=headers)
print(r.json())
# Send as Multipart Form-Data
<?php
// Generate a boundary string
$boundary = uniqid();
// Open the file
$file = file_get_contents('test.png');
// Build the multipart form data
$data = '';
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="file"; filename="test.png"' . "\r\n";
$data .= "Content-Type: image/png\r\n\r\n";
$data .= $file . "\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="fileName"' . "\r\n\r\n";
$data .= "test.png\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="description"' . "\r\n\r\n";
$data .= "best image\r\n";
$data .= "--" . $boundary . "--\r\n";
// Setup the context for the request
$options = [
'http' => [
'method' => 'POST',
'header' => "Authorization: Bearer API_KEY\r\n" .
"Content-Type: multipart/form-data; boundary=" . $boundary . "\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
]
];
// Send the request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// Print the response
echo $result;
# Send as Base64
<?php
$data = [
'file' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...',
'fileName' => "test.png",
'description' => "best image"
];
$ch = curl_init('https://api.ayrshare.com/api/media/upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer API_KEY'
]);
$response = curl_exec($ch);
curl_close($ch);
echo json_encode(json_decode($response), JSON_PRETTY_PRINT);
{
"id": "1167335b-6c37-4fc6-ab8a-044e0005d335-jpeg",
"url": "https://images.ayrshare.com/q3Ls85VTsrbODnGIJHpy7PaHWwA3/1167335b-6c37-4fc6-ab8a-044ed885d.jpeg",
"fileName": "fun.jpg",
"description": "good times"
}
{
"action": "request",
"status": "error",
"code": 101,
"message": "Missing or incorrect parameters. Please verify with the docs. .../ayrshare.com/rest-api/endpoints"
}
Este endpoint permite fazer upload de um arquivo, uma imagem ou um vídeo pequeno para incluir em sua publicação. Será retornada a URL da imagem que pode ser usada no endpoint /post.
Você pode enviar o arquivo como multipart form data como parâmetro de formulário ou como arquivo codificado em Base64 como parâmetro do corpo.
Notas importantes sobre uploads de mídia:
-
Para melhor desempenho, recomendamos
- Hospedar arquivos de mídia em seu próprio servidor (por exemplo, AWS S3).
- Passar a URL da mídia diretamente no parâmetro
mediaUrlsdo endpoint /post. - Essa abordagem é mais rápida do que fazer upload de arquivos por este endpoint.
-
Retenção de arquivos de mídia
- Arquivos enviados são armazenados por 90 dias.
- Após 90 dias:
- Publicações já publicadas nas redes sociais não são afetadas.
- Publicações agendadas falharão ao publicar se referenciarem mídia expirada.
-
Limites de tamanho de arquivo
- Tamanho máximo do arquivo: 30 MB.
- Para arquivos maiores, consulte nosso guia sobre como lidar com uploads de mídia grandes.
Se você já tem sua mídia acessível por uma URL externa, como um bucket S3, é possível pular o upload dos arquivos para a Ayrshare. Basta fazer um POST para o endpoint
/post com sua URL acessível externamente no parâmetro mediaURLs do corpo e o arquivo será enviado automaticamente.Parâmetros do header
Use
multipart/form-data se estiver enviando dados de formulário multipart - veja abaixo. Caso contrário, envie o padrão application/json.Parâmetros do body
Tamanho máximo do arquivo de 30 MB.Recomendamos enviar como um objeto multipart form-data em vez de codificação Base64.
O nome do arquivo a ser enviado.
Uma descrição do arquivo.
Enviar como Multipart Form-Data
Envie o arquivo de mídia como um objeto multipart form-data. Certifique-se de especificar oContent-Type conforme mencionado acima.
Enviar como Base64
Envie o arquivo de mídia como uma string codificada em Base64 no formato Data URI. A string deve começar comdata:content/type;base64
Exemplos de codificação com formato de saída Data URI:
Observação: o endpoint /post aceita arquivos maiores via uma URL externa com o parâmetro mediaUrls.
# Send as Multipart Form-Data
curl \
-H "Authorization: Bearer API_KEY" \
-F "file=@test.png" \
-F "fileName=test.png" \
-F "description=best image" \
-X POST https://api.ayrshare.com/api/media/upload
# Send as Base64
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://api.ayrshare.com/api/media/upload
// Send as Multipart Form-Data
const FormData = require('form-data');
const fs = require('fs');
const API_KEY = "API_KEY";
const imagePath = './test.png';
const form = new FormData();
form.append('file', fs.createReadStream(imagePath));
form.append('fileName', 'test.png');
form.append('description', 'best image');
fetch("https://api.ayrshare.com/api/media/upload", {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`
// Don't set Content-Type header - FormData will set it automatically with boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Send as Base64
const API_KEY = "API_KEY";
const base64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...";
fetch("https://api.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);
# Send as Multipart Form-Data
import requests
# For a local file:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Form data
data = {
'fileName': 'test.png',
'description': 'best image'
}
headers = {
'Authorization': 'Bearer API_KEY'
}
r = requests.post(
'https://api.ayrshare.com/api/media/upload',
files=files,
data=data,
headers=headers
)
print(r.json())
# Send as Base64
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://api.ayrshare.com/api/media/upload',
json=payload,
headers=headers)
print(r.json())
# Send as Multipart Form-Data
<?php
// Generate a boundary string
$boundary = uniqid();
// Open the file
$file = file_get_contents('test.png');
// Build the multipart form data
$data = '';
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="file"; filename="test.png"' . "\r\n";
$data .= "Content-Type: image/png\r\n\r\n";
$data .= $file . "\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="fileName"' . "\r\n\r\n";
$data .= "test.png\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= 'Content-Disposition: form-data; name="description"' . "\r\n\r\n";
$data .= "best image\r\n";
$data .= "--" . $boundary . "--\r\n";
// Setup the context for the request
$options = [
'http' => [
'method' => 'POST',
'header' => "Authorization: Bearer API_KEY\r\n" .
"Content-Type: multipart/form-data; boundary=" . $boundary . "\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
]
];
// Send the request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// Print the response
echo $result;
# Send as Base64
<?php
$data = [
'file' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...',
'fileName' => "test.png",
'description' => "best image"
];
$ch = curl_init('https://api.ayrshare.com/api/media/upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer API_KEY'
]);
$response = curl_exec($ch);
curl_close($ch);
echo json_encode(json_decode($response), JSON_PRETTY_PRINT);
{
"id": "1167335b-6c37-4fc6-ab8a-044e0005d335-jpeg",
"url": "https://images.ayrshare.com/q3Ls85VTsrbODnGIJHpy7PaHWwA3/1167335b-6c37-4fc6-ab8a-044ed885d.jpeg",
"fileName": "fun.jpg",
"description": "good times"
}
{
"action": "request",
"status": "error",
"code": 101,
"message": "Missing or incorrect parameters. Please verify with the docs. .../ayrshare.com/rest-api/endpoints"
}
⌘I
