File Upload API Documentation

Endpoint

URL: https://cdn.nagadwallet.net/api/files/upload

Method: POST

Headers

The request should include the following header:

Header Name Type Description
x-api-key string Your API key for authenticating the upload request.

Request Body

The request should include the following fields:

Field Name Type Description
project string The name or identifier of the project.
fileType string The type of the file being uploaded (e.g., image, pdf, etc.).
file binary The binary content of the file to be uploaded.

The request should use the multipart/form-data content type.

Response

On a successful upload, the API will return:

{
    "message": "File uploaded successfully",
    "fileUrl": "https://cdn.nagadwallet.net/uploads/project/fileType/filename.ext"
}

The fileUrl field contains the URL of the uploaded file. To access the file URL, you must include a Bearer token in the Authorization header as shown below.

Headers for Accessing File URL

Header Name Type Description
Authorization string Bearer token for accessing the file URL. Format: Bearer <token>.

Example Usage in React using axios

Here is an example of how you can use the API to upload a file in a React application:

import React, { useState } from 'react';
import axios from 'axios';

function App() {
  const [file, setFile] = useState(null);

  const handleFileChange = (e) => {
    setFile(e.target.files[0]);
  };

  const handleUpload = async () => {
    const formData = new FormData();
    formData.append('project', 'my-project');
    formData.append('fileType', 'image');
    formData.append('file', file);

    try {
      const res = await axios.post('https://cdn.nagadwallet.net/api/files/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'x-api-key': 'your-api-key-here' // Replace with your API key
        }
      });

      console.log(res.data);

      // Example of accessing the file URL
      const fileUrl = res.data.fileUrl;
      const fileAccessRes = await axios.get(fileUrl, {
        headers: {
          'Authorization': 'Bearer your-bearer-token-here' // Replace with your Bearer token
        }
      });

      console.log(fileAccessRes.data);
    } catch (err) {
      console.error(err);
    }
  };


}

export default App;
    

In this example, the user can select a file using an input element, and then click a button to upload the file to the API. The file is sent as binary data in a FormData object, with the necessary API key included for authentication. To access the file URL, the Bearer token must be included in the Authorization header.