What You’ll Build
An Express server that:- Proxies Decart API requests from the browser
- Keeps your API key secure on the server
- Requires zero changes to client-side SDK code
- Serves a simple frontend demo
Architecture
/api/decart, which securely attaches your API key and forwards them to Decart’s API.
Prerequisites
- Node.js 18 or higher
- A Decart API key
Setup
Start the server
Server Code
Thesrc/server.ts file sets up the proxy middleware:
Key Concepts
Proxy Middleware
The proxy middleware intercepts requests and forwards them with your API key:routeis the path to mount the proxy (defaults to/api/decart)handler()creates the middleware that forwards requests
DECART_API_KEY from process.env. You can also pass it explicitly:
Client-Side Code
In your frontend, use the SDK with the proxy route:Security Benefits
- API Key Never Exposed - The key stays on the server
- No Client-Side Secrets - Browsers never see sensitive data
- Full Control - Add authentication, rate limiting, or logging
- Simple Migration - Works with existing SDK code
Using with ES Modules
The example uses native ES modules in the browser:Using with CDN
Alternatively, load the SDK from a CDN:Adding Authentication
Add your own authentication layer:Rate Limiting
Add rate limiting to prevent abuse:Logging Requests
Log all proxy requests:Custom Proxy Route
Change the proxy route path:Production Deployment
Environment Variables
SetDECART_API_KEY in your production environment:
CORS Configuration
For production, configure CORS properly:HTTPS
Always use HTTPS in production to protect API requests.Frontend Frameworks
React Example
Vue Example
Troubleshooting
CORS Errors
If you see CORS errors, ensure your Express app is properly configured:Module Not Found
If the SDK module isn’t found, ensure you’ve built the packages:401 Unauthorized
Check thatDECART_API_KEY is set in your .env file.