# 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
上传图片或视频
上传图片或小型视频文件以用于你的帖子
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"
}
此端点允许你上传一个文件、图片或小型视频以用于你的帖子。将返回该图片的 URL,可在 /post 端点中使用。
你可以将文件作为 multipart form data 的表单参数,或作为 Base64 编码文件 的请求体参数进行传递。
有关媒体上传的重要说明:
-
为获得最佳性能,我们建议
- 将媒体文件托管在你自己的服务器上(例如 AWS S3)。
- 直接在 /post 端点的
mediaUrls参数中传入媒体 URL。 - 这种方式比通过此端点上传文件更快。
-
媒体文件保留
- 已上传的文件保留 90 天。
- 90 天后:
- 已发布到社交网络的帖子不会受到影响。
- 如果定时帖子引用了已过期的媒体,将无法发布。
-
文件大小限制
- 最大文件大小:30 MB。
- 对于更大的文件,请参见我们的处理大型媒体上传指南。
如果你的媒体已经可以通过外部 URL 访问(例如 S3 存储桶),可以跳过将文件上传到 Ayrshare 的步骤。只需在 POST 到
/post 端点时将外部可访问的 URL 放入 mediaURLs 请求体参数中,你的文件就会被自动上传。Header Parameters
如果发送 multipart form data,请使用
multipart/form-data,详见下文。否则,发送标准的 application/json。Body Parameters
最大文件大小 30 MB。我们建议以 multipart form-data 对象形式发送,而不是 Base64 编码。
要上传的文件的名称。
文件的描述。
以 Multipart Form-Data 形式发送
以 multipart form-data 对象形式发送媒体文件。请务必按上文所述指定Content-Type。
以 Base64 形式发送
将媒体文件以 Base64 编码字符串的形式作为 Data URI 字符串发送。该字符串应以data:content/type;base64 开头。
以 Data URI 为输出格式的编码示例:
注意:/post 端点支持通过 mediaUrls 参数使用外部 URL 传入更大的文件。
# 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
