JavaScript

JavaScript is a general-purpose programming language that is widely used for web development. For developers familar with Python, simple JavaScript data sources should be very accessible.

Evidence supports JavaScript data sources, which allow you to connect to APIs, transform and load data into Evidence using JavaScript.

Add a New JavaScript Data Source

  1. Start the Evidence dev server: npm run dev or Start Evidence using the VSCode extension.
  2. Navigate to the settings page, also accessible via the ... menu in the top right.
  3. Click the New Source button.
  4. Select JavaScript as the data source type.
  5. Enter the configuration options for the connection.
  6. Click the Test button to confirm the connection is successful.
  7. Click the Save button to save the connection.
  8. Your configuration options are saved in two files:
    • /sources/[source_name]/connections.yaml: Non-sensitive values, source controlled.
    • /sources/[source_name]/connection.options.yaml: Sensitive values, not source controlled, base-64 encoded.

Then, add a .js file to the sources/[your_source_name]/ folder. The file must export an object named data:

sources/[your_source_name]/pokedex.js

let url = 'https://pokeapi.co/api/v2/pokemon/';

const response = await fetch(url);
const json = await response.json();
const data = json.results;

// Export the data object
export { data };

Configuration

If connecting to an API, you will likely need to add an API key or some other form of authentication.

Credentials

Most APIs require some form of authentication. Evidence supports passing credentials via environment variables to your JS file. They must be prefixed with EVIDENCE_.

You can pass credentials via environment variables to your JS file. They must be prefixed with EVIDENCE_.

In your local environment, you can set these variables in the .env file.

For production, you will need to add these variables to your build environment.

EVIDENCE_API_KEY=1234567890
let key = process.env.EVIDENCE_API_KEY;
let url = 'https://whatever.com/api';

const response = await fetch(url, {
	headers: {
		'x-api-key': key
	}
});

const json = await response.json();
const data = json.results;

export { data };

JavaScript Type Support

Type Supported
String Yes
Number Yes
Boolean Yes
Date Yes
Array Partial*
Object No**

*Arrays are converted to strings. eg [1, 2, 3]"1,2,3"