Skip to main content

Python SDK for UploadThing

Project description

Installation

pip install uploadthing.py

Quickstart

Using UTApi

This is basically a 1:1 clone of the official TypeScript SDK

import asyncio, os

from uploadthing_py import UTApi


async def main():
    utapi = UTApi(os.getenv("UPLOADTHING_SECRET"))

    # List the files in your app
    res = await utapi.list_files()
    print("List files:", res)

    # Delete the first file from the list
    key = res[0].key
    res = await utapi.delete_file(key)
    print("Delete file:", res)

    # (TODO) Create a new file
    # res = await utapi.upload_files()


if __name__ == "__main__":
    asyncio.run(main())

Using FastAPI

You can use FastAPI like any of the JavaScript backend adapters.

[!TIP]

You can use this example along with one of the client examples

UPLOADTHING_SECRET=sk_foo poetry run uvicorn examples.fastapi:app --reload --port 3000

[!WARNING]

This is a work in progress and not yet ready for production use.

from fastapi import FastAPI, Request, Response
from uploadthing_py import (
    UploadThingRequestBody,
    create_uploadthing,
    create_route_handler,
)
from fastapi.middleware.cors import CORSMiddleware
import os

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

f = create_uploadthing()


upload_router = {
    "videoAndImage": f(
        {
            "image/png": {"max_file_size": "4MB"},
            "image/heic": {"max_file_size": "16MB"},
        }
    )
    .middleware(lambda req: {"user_id": req.headers["x-user-id"]})
    .on_upload_complete(lambda file, metadata: print(f"Upload complete for {metadata['user_id']}"))
}
handlers = create_route_handler(
    router=upload_router,
    api_key=os.getenv("UPLOADTHING_SECRET"),
    is_dev=os.getenv("ENVIRONMENT", "development") == "development",
)


@app.get("/api")
async def greeting():
    return "Hello from FastAPI"


@app.get("/api/uploadthing")
async def ut_get():
    return handlers["GET"]()


@app.post("/api/uploadthing")
async def ut_post(
    request: Request,
    response: Response,
    body: UploadThingRequestBody,
):
    return await handlers["POST"](
        request=request,
        response=response,
        body=body,
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uploadthing_py-0.3.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uploadthing_py-0.3.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file uploadthing_py-0.3.3.tar.gz.

File metadata

  • Download URL: uploadthing_py-0.3.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1021-azure

File hashes

Hashes for uploadthing_py-0.3.3.tar.gz
Algorithm Hash digest
SHA256 d7df29501a3ffdb09fe1a73f3782c46d6ebe67c322d42fbe4b5da411cb83abdf
MD5 b8ea3c2712d020b33f7105ccebd9db9b
BLAKE2b-256 e3c1e9c8263f2c88bd4845cdb70db79571c6b656aaec20a69847c21c42bceaaf

See more details on using hashes here.

File details

Details for the file uploadthing_py-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: uploadthing_py-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1021-azure

File hashes

Hashes for uploadthing_py-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1798c233b07d4bbcb8c7339acb65e50834295eaa8e498791e03f90e88baa778a
MD5 402bae2d483adb9ec9e4407883ad9a64
BLAKE2b-256 96c0d479aba8db3c73a24543030b016c45e171e05d1ea6c5a4dafcfd29eaeb09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page