Working with Files¶
Uploading¶
To upload a file to Apisful, send a POST request to the https://api.apisful.com/v1/files/
. The request must contain the Content-Type: multipart/form-data
header. Keep in mind that files are limited to 20 megabytes.
curl -X POST \
-H "X-Api-Key: {your_key_here}" \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/file/my-image.jpg" \
https://api.apisful.com/v1/files/
// [!] we use https://github.com/axios/axios lib in this example
// create an axios instance with default config for all other requests
const API = axios.create({
baseURL: 'https://api.apisful.com/v1/',
headers: {
'X-Api-Key': '{your_key_here}'
}
});
// add content-type header
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
// create request body
const formData = new FormData()
formData.append('file', file)
// make a file uploading request
API.post('files/', formData, config)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
When the file upload is successful, the HTTP response is a 201 Created
and the Location
header which contains the URL for the file:
Status: 201 Created
Location: https://api.apisful.com/v1/files/21/b99356f64eb8c145c783b8e9f15c8775/my-image.jpg
The response body is a JSON object containing a link to the uploaded file.
{
"file":"https://api.apisful.com/v1/files/21/b99356f64eb8c145c783b8e9f15c8775/my-image.png"
}
Associating with records¶
After a file is uploaded, you are free to use the file's url everywhere you need it. Let's update an image of our product we created before.
PUT https://api.apisful.com/v1/collections/products/54874/
Content-Type: application/json;charset=UTF-8
X-Api-Key: {your_key_here}
{
"id": 54874,
"title": "A new title",
"price": 17.99,
"sizes": "S, M, L, XL",
"image": "https://api.apisful.com/v1/files/21/b99356f64eb8c145c783b8e9f15c8775/my-image.png",
"created_at": "2020-07-10T11:15:31.867705Z",
"updated_at": "2020-07-10T11:15:31.867705Z"
}
curl -X PUT \
-H "X-Api-Key: {your_key_here}" \
-H "Content-Type: application/json;charset=UTF-8" \
-d '{ "id": 54874, "title": "A new title", "price": 17.99, "sizes": "S, M, L, XL", "image": "https://api.apisful.com/v1/files/21/b99356f64eb8c145c783b8e9f15c8775/my-image.png", "created_at": "2020-07-10T11:15:31.867705Z", "updated_at": "2020-07-10T11:15:31.867705Z" }' \
https://api.apisful.com/v1/collections/products/54874/
// [!] we use https://github.com/axios/axios lib in this example
// create an axios instance with default config for all other requests
const API = axios.create({
baseURL: 'https://api.apisful.com/v1/',
headers: {
'X-Api-Key': '{your_key_here}'
}
});
// make a record updating request
API.put('collections/products/54874/', {
id: 54874,
title: "A new title",
price: 17.99,
sizes: "S, M, L, XL"
image: "https://api.apisful.com/v1/files/21/b99356f64eb8c145c783b8e9f15c8775/my-image.png"
created_at: "2020-07-10T11:15:31.867705Z",
updated_at: "2020-07-10T11:15:31.867705Z"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});