StaticFiles.
Installing Dependencies
StaticFiles requires aiofiles to be installed:
If you installed FastAPI with
pip install fastapi[standard], aiofiles is already included.Using StaticFiles
ImportStaticFiles 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
static/css/styles.css→http://localhost:8000/static/css/styles.cssstatic/js/script.js→http://localhost:8000/static/js/script.jsstatic/images/logo.png→http://localhost:8000/static/images/logo.png
Complete Example
Mount Parameters
Themount() method accepts:
- path: The URL path where static files will be served (e.g.,
/static) - app: The
StaticFilesinstance - 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 With
index.html for directory requests. Default is False.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
Whenhtml=True, StaticFiles will serve index.html files for directory URLs:
/→ servesstatic/index.html/about/→ servesstatic/about/index.html
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 thename 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
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.
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