Skip to main content
You can serve static files (like images, CSS, JavaScript, etc.) automatically from a directory using StaticFiles.

Installing Dependencies

StaticFiles requires aiofiles to be installed:
If you installed FastAPI with pip install fastapi[standard], aiofiles is already included.

Using StaticFiles

Import StaticFiles and mount it to your application:

How It Works

1

Create Directory

Create a directory for your static files (e.g., static/) in your project.
2

Mount StaticFiles

Mount StaticFiles at a specific path (e.g., /static).
3

Access Files

Files in the directory become accessible at the mounted path.

Example Structure

With this structure:
  • static/css/styles.csshttp://localhost:8000/static/css/styles.css
  • static/js/script.jshttp://localhost:8000/static/js/script.js
  • static/images/logo.pnghttp://localhost:8000/static/images/logo.png

Complete Example

Mount Parameters

The mount() method accepts:
  • path: The URL path where static files will be served (e.g., /static)
  • app: The StaticFiles instance
  • name: Internal name for the mount (used for URL generation)

StaticFiles Parameters

1

directory

The directory path containing your static files. Can be absolute or relative.
2

html (optional)

Whether to serve index.html for directory requests. Default is False.
With html=True, requesting /static/docs/ will serve /static/docs/index.html if it exists.
3

check_dir (optional)

Whether to check if the directory exists at startup. Default is True.

Serving index.html

When html=True, StaticFiles will serve index.html files for directory URLs:
With this configuration:
  • / → serves static/index.html
  • /about/ → serves static/about/index.html
When mounting at /, static files take precedence over API routes. Mount static files after defining your API routes, or use a different path like /static.

Best Practices

1

Mount Path

Use a dedicated path like /static or /assets to avoid conflicts with API routes:
2

Directory Organization

Organize static files by type:
3

Production Serving

In production, consider using a CDN or web server (like Nginx) to serve static files for better performance.

URL Generation

Use the name parameter to generate URLs to static files:

Multiple Static Directories

You can mount multiple static file directories:

File Downloads

Static files are served with appropriate content types. Browsers will display images, videos, etc., and download other file types based on the MIME type. To force download of a specific file, create a dedicated endpoint:

Security Considerations

Be careful with static file directories:
  • Don’t serve directories containing sensitive files
  • Don’t use user input to construct file paths (path traversal attacks)
  • Keep static files separate from application code
  • Consider setting appropriate cache headers

Cache Headers

StaticFiles automatically sets cache headers. For custom cache control:

Error Handling

If a requested file doesn’t exist, StaticFiles returns a 404 error automatically.
You don’t need to handle 404 errors for static files - it’s handled automatically by StaticFiles.

Production Deployment

For production, consider these alternatives:
1

Web Server

Use Nginx or Apache to serve static files directly:
2

CDN

Upload static files to a CDN (CloudFront, CloudFlare, etc.) for global distribution and better performance.
3

Cloud Storage

Store static files in cloud storage (S3, Google Cloud Storage, etc.) and serve them directly from there.
While FastAPI can serve static files during development, using a dedicated web server or CDN in production provides better performance, caching, and scalability.

Common Issues

Directory Not Found

If you see “Directory ‘static’ does not exist”:

404 for Static Files

Check:
  • File actually exists in the directory
  • Path is correct (case-sensitive on Linux/Mac)
  • Directory is mounted before starting the server
  • No conflicting routes