# Gửi dưới dạng 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
# Gửi dưới dạng 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
// Gửi dưới dạng 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}`
// Không đặt header Content-Type - FormData sẽ tự động đặt nó với boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Gửi dưới dạng 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);
# Gửi dưới dạng Multipart Form-Data
import requests
# Đối với tệp cục bộ:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Dữ liệu form
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())
# Gửi dưới dạng 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())
# Gửi dưới dạng Multipart Form-Data
<?php
// Tạo chuỗi boundary
$boundary = uniqid();
// Mở tệp
$file = file_get_contents('test.png');
// Xây dựng dữ liệu multipart form
$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";
// Thiết lập context cho 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
]
];
// Gửi request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// In phản hồi
echo $result;
# Gửi dưới dạng 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
Tải lên Hình ảnh hoặc Video
Tải lên một hình ảnh hoặc tệp video nhỏ để đưa vào bài đăng của bạn
POST
/
media
/
upload
# Gửi dưới dạng 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
# Gửi dưới dạng 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
// Gửi dưới dạng 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}`
// Không đặt header Content-Type - FormData sẽ tự động đặt nó với boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Gửi dưới dạng 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);
# Gửi dưới dạng Multipart Form-Data
import requests
# Đối với tệp cục bộ:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Dữ liệu form
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())
# Gửi dưới dạng 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())
# Gửi dưới dạng Multipart Form-Data
<?php
// Tạo chuỗi boundary
$boundary = uniqid();
// Mở tệp
$file = file_get_contents('test.png');
// Xây dựng dữ liệu multipart form
$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";
// Thiết lập context cho 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
]
];
// Gửi request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// In phản hồi
echo $result;
# Gửi dưới dạng 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"
}
Endpoint này cho phép bạn tải lên một tệp hoặc một hình ảnh hoặc video nhỏ để đưa vào bài đăng của bạn. URL đến hình ảnh sẽ được trả về, có thể được sử dụng trong endpoint /post.
Bạn có thể truyền tệp dưới dạng multipart form data như một tham số form hoặc dưới dạng tệp mã hóa Base64 như một tham số body.
Lưu ý quan trọng về việc tải lên media:
-
Để có hiệu suất tốt nhất, chúng tôi khuyến nghị
- Lưu trữ các tệp media trên máy chủ của riêng bạn (ví dụ AWS S3).
- Truyền trực tiếp URL media trong tham số
mediaUrlscủa endpoint /post. - Cách tiếp cận này nhanh hơn so với việc tải lên tệp thông qua endpoint này.
-
Lưu giữ tệp media
- Các tệp đã tải lên được lưu trữ trong 90 ngày.
- Sau 90 ngày:
- Các bài đăng đã xuất bản trên mạng xã hội không bị ảnh hưởng.
- Các bài đăng đã lên lịch sẽ không xuất bản được nếu chúng tham chiếu đến media đã hết hạn.
-
Giới hạn kích thước tệp
- Kích thước tệp tối đa: 30 MB.
- Đối với các tệp lớn hơn, xem hướng dẫn của chúng tôi về xử lý tải lên media lớn.
Nếu bạn đã có media có thể truy cập bằng URL bên ngoài, chẳng hạn như một S3 bucket, bạn có thể bỏ qua việc tải các tệp lên Ayrshare. Chỉ cần POST đến endpoint
/post với URL có thể truy cập từ bên ngoài của bạn trong tham số body mediaURLs và tệp của bạn sẽ tự động được tải lên.Tham số Header
Sử dụng
multipart/form-data nếu gửi dữ liệu multipart form - xem bên dưới. Nếu không, gửi application/json chuẩn.Tham số Body
Kích thước tệp tối đa 30 MB.Chúng tôi khuyến nghị gửi dưới dạng đối tượng multipart form-data thay vì mã hóa Base64.
Tên của tệp sẽ được tải lên.
Mô tả của tệp.
Gửi dưới dạng Multipart Form-Data
Gửi tệp media dưới dạng đối tượng multipart form-data. Hãy đảm bảo chỉ địnhContent-Type như đã đề cập ở trên.
Gửi dưới dạng Base64
Gửi tệp media dưới dạng chuỗi được mã hóa Base64 dưới dạng chuỗi Data URI. Chuỗi phải bắt đầu bằngdata:content/type;base64
Ví dụ mã hóa với Định dạng Đầu ra Data URI:
- Hình ảnh: https://base64.guru/converter/encode/image
- Video: https://base64.guru/converter/encode/video
mediaUrls.
# Gửi dưới dạng 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
# Gửi dưới dạng 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
// Gửi dưới dạng 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}`
// Không đặt header Content-Type - FormData sẽ tự động đặt nó với boundary
},
body: form
})
.then(res => res.json())
.then(json => console.log(json))
.catch(console.error);
// Gửi dưới dạng 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);
# Gửi dưới dạng Multipart Form-Data
import requests
# Đối với tệp cục bộ:
files = {
'file': ('test.png', open('test.png', 'rb')),
}
# Dữ liệu form
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())
# Gửi dưới dạng 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())
# Gửi dưới dạng Multipart Form-Data
<?php
// Tạo chuỗi boundary
$boundary = uniqid();
// Mở tệp
$file = file_get_contents('test.png');
// Xây dựng dữ liệu multipart form
$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";
// Thiết lập context cho 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
]
];
// Gửi request
$context = stream_context_create($options);
$result = file_get_contents('https://api.ayrshare.com/api/media/upload', false, $context);
// In phản hồi
echo $result;
# Gửi dưới dạng 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
