Our API allows users to upload files from the frontend application. To do so securely, the user should:
curl --location 'https://simple-upload1.p.rapidapi.com/upload/one-time' \
--header 'Content-Type: application/json' \
--header 'X-RapidAPI-Key': '`YOUR_RAPID_API_KEY',\
--data '{
"type": "Image"
}
// This is the URL generated in step 1
const oneTimeUploadURL = "{GENERATED_URL_FROM_STEP_1}";
const fileInput = document.getElementById("image-input");
if(!fileInput || !fileInput.files?.[0]) throw new Error("Please select a file first")
const formData = new FormData();
formData.append("file", fileInput.files[0]);
const response = await fetch(oneTimeUploadURL, { body: formData, method: "POST" });
if(response.status === 200) {
// The file uploaded succesfully
const responseObj = await response.json();
// You can now save this URL on your database and use as a reference to your images/audios on your website
const myDownloadURL = responseObj.url;
} else if(status < 500){
// Bad request (e.g: File size cannot exceed 10 MB)
const { reason } = await response.json();
throw new Error("Failed to upload the file for the following reason: " + reason);
} else {
// Some unexpected error happened. Please report these errors to us, so we can ensure they do not occur again
throw new Error("Unexpected error: "+ await response.text());
}
<img width="100" src="https://rapidapi-storage.easier-api.com/public/test/randomTest/fdb4eab8-6765-ec33-adf3-27011ea42d04.jpg">