Simple-Upload

פרמיום
על ידי Omar Foudane | מְעוּדכָּן 2달 전 | Data
פּוֹפּוּלָרִיוּת

7.2 / 10

חֶבִיוֹן

76ms

רמת שירות

100%

Health Check

N/A

חזרה לכל ההדרכות (1)

How to upload an image

Our API allows users to upload files from the frontend application. To do so securely, the user should:

  1. Generate a one-time usage URL from our Rapid API endpoint /upload/one-time:
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"
} 
  1. The API will generate a link that can be used one time to upload 1 file. The generated link does not go through Rapid API platform, so no need to attach any secret credentials to it. The URL is protected by a long token. Following is an example using javascript on “File” input field with the id “image-input”
// 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());
}
  1. Use the download URL in your website. For instance, if the uploaded file is an image, then you can use the generated URL in step 2 in the src attribute. Here is an example: <img width="100" src="https://rapidapi-storage.easier-api.com/public/test/randomTest/fdb4eab8-6765-ec33-adf3-27011ea42d04.jpg">