Use Cases
- Fetch data from REST APIs
- Web scraping
- Custom data transformations
- Combine multiple data sources
- Access services without native connectors
Setup
Add JavaScript Source
Navigate to Settings in your Evidence app (localhost:3000/settings) and add a JavaScript datasource.
Create JavaScript Files
Create
.js files in your sources/javascript/ directory. Each file represents a table.Basic Example
Create a file likesources/javascript/pokedex.js:
pokedex
Using Environment Variables
Pass credentials via environment variables prefixed withEVIDENCE_:
.env file:
Type Support
| JavaScript Type | Supported | Notes |
|---|---|---|
| String | ✅ Yes | Converted to Evidence string type |
| Number | ✅ Yes | Converted to Evidence number type |
| Boolean | ✅ Yes | Converted to Evidence boolean type |
| Date | ✅ Yes | Converted to Evidence date type |
| Array | ⚠️ Partial | Arrays are converted to strings (e.g., [1, 2, 3] → "1,2,3") |
| Object | ❌ No | Objects display as [object Object] |
Advanced Examples
Pagination
Handle paginated APIs:Data Transformation
Transform API responses into the format you need:Multiple API Calls
Combine data from multiple endpoints:Error Handling
Include error handling for production use:Working with CSV/JSON Files
Read local files using Node.js:Best Practices
Use Environment Variables
Never hardcode API keys or secrets. Use environment variables with the
EVIDENCE_ prefix.Handle Errors
Include try/catch blocks and provide fallback empty arrays to prevent build failures.
Flatten Complex Data
Convert nested objects and arrays to flat structures before exporting.
Optimize API Calls
Implement pagination and caching to avoid rate limits and improve performance.
Troubleshooting
Cannot use import statement outside a module
Cannot use import statement outside a module
Ensure your file uses
.mjs extension or has "type": "module" in package.json. Evidence supports both CommonJS and ES modules.fetch is not defined
fetch is not defined
Use Node.js 18+ which includes native fetch support, or import node-fetch:
Data not showing in queries
Data not showing in queries
Verify that:
- You’re exporting a
datavariable - The data is an array of objects
- You’ve run
npm run sourcesafter creating/modifying the file - The filename matches your SQL FROM clause
Environment variables not working
Environment variables not working
Make sure:
- Variables are prefixed with
EVIDENCE_ - Your
.envfile is in the project root - You’ve restarted the dev server after adding environment variables
Limitations
- JavaScript files are executed at build time (when you run
npm run sources), not at query time - The data is static once generated - you need to rebuild sources to refresh
- Heavy processing should be optimized as it runs during the build
- Some Node.js APIs may not be available in all deployment environments
Related
- Faker Datasource - Generate mock data for testing
- CSV Datasource - Import CSV files
- Environment Variables - Configure credentials securely