Add an API to your existing app
Ask Agent to create a REST API for your database. Agent will set up endpoints, add authentication, and protect your data.1
Open your app
Open the app that contains the database you want to share.
2
Ask Agent to create the API
3
Add your API key
- Open the Secrets tab
- Add a secret:
API_KEYwith a secure password - This key protects your database from unauthorized access
4
Publish your app
- Select Run to test your API
- Select Publish > Autoscale to get a public URL
- Your database is now accessible to other apps
Example: Database API template
See a working example with the Database API Example template. Fork it to start a new database API from scratch, or explore the code to understand how it works. The template includes:- Automatic database setup with sample data
- API endpoints for all CRUD operations
- API key authentication
- Drizzle ORM for safe queries
1
Fork the template
Open the Database API Example template and fork it.
2
Add an API key
- Open the Secrets tab
- Add
API_KEYwith a secure password
3
Run and publish
- Select Run to start the API
- Select Publish > Autoscale when ready
Connect from other apps
Ask Agent to write the connection code in any of your other apps:API endpoints reference
Your database uses standard REST endpoints. For anitems table:
| Action | Method | Endpoint |
|---|---|---|
| List all | GET | /api/items |
| Get one | GET | /api/items/:id |
| Create | POST | /api/items |
| Update | PUT | /api/items/:id |
| Delete | DELETE | /api/items/:id |
X-API-KEY header with your API key.
Write your own connection code
If you prefer to code the connection yourself, use these backend examples:Add more tables
Extend your database as your app grows:How it works
Your API acts as a central hub. When an app needs data, it sends a request to your API. The API checks the API key, then reads or writes to your database. Multiple apps share one database securely.Security
Your API includes built-in protection:- API key authentication: Only apps with the correct key can access your database
- SQL injection protection: Drizzle ORM prevents malicious queries
- Data validation: The API checks all data before saving
Troubleshooting
Can't connect from another app
Can't connect from another app
- Publish your database API app (not just run it)
- Use your published URL ending in
.replit.app - Verify both apps have the same API key in Secrets
- Check the secret is named
API_KEYin both apps
Authentication errors
Authentication errors
- Store the API key in Secrets, not in code
- Verify the API key matches in both apps
- Check the secret is named
API_KEY(all caps)
Need help
Need help
Ask Agent to troubleshoot:
Next steps
- Database - Create and manage databases
- Connect to a database - Learn connection methods
- Production databases - Set up production databases