How to expose an API for file upload on RapidAPI?

I have developed an API using FastAPI that accepts a Word document upload and extracts its contents. This API is currently hosted on Heroku and functions properly with the following implementation. Below is the code for the API, which successfully retrieves the desired information.

API Implementation:

import os
import uvicorn
from fastapi import FastAPI, UploadFile, File
import docx
from io import BytesIO

app = FastAPI()

@app.post("/extractdoc")
async def extractdoc(file: UploadFile = File(...)):
    document = docx.Document(BytesIO(await file.read()))
    text_content = []
    for paragraph in document.paragraphs:
        text_content.append(paragraph.text)
    return '\n'.join(text_content)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Test Client Example:

import requests

file_path = "path_to_your_word_document"
files = {'file': (file_path, open(file_path, 'rb'))}

response = requests.post('https://extractdoc.herokuapp.com/extractdoc', files=files)
print(response.json())

Now, I’m interested in integrating this API with RapidAPI, but I’m having trouble figuring out how to connect the /extractdoc endpoint for file uploads. The options I came across just mention JSON and data requests. Is it possible to handle file uploads?

Yes, integrating your FastAPI with RapidAPI for handling file uploads is indeed feasible. When you publish your API on RapidAPI, you can configure your API endpoints to accept multipart/form-data requests, which is the format for file uploads.

On the RapidAPI dashboard, ensure that your API definition reflects the multipart/form-data content type. You might want to specify in the endpoint configuration that the parameter to be used for the file is of type file. This will instruct RapidAPI to handle the requests properly. Furthermore, don’t forget to test the endpoint using RapidAPI’s testing functionality once it’s set up to ensure everything is working correctly. With these steps, your file upload endpoint should be ready for integration on RapidAPI.