# 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 請求主體參數,您的檔案就會被自動上傳。標頭參數
若傳送 multipart form data,請使用
multipart/form-data — 請參見下方。否則使用標準的 application/json。主體參數
最大檔案大小 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
