Requirements

As best practice, it is recommended to set up your environment variables. Create a .env file in your project root and add the following:
VALENCE_API_KEY=your_api_key                    # Required: Your Valence API key
VALENCE_DISCRETE_URL=https://discrete-api-url   # Optional: Discrete audio endpoint
VALENCE_ASYNCH_URL=https://asynch-api-url        # Optional: Asynch audio endpoint  
VALENCE_LOG_LEVEL=info                          # Optional: debug, info, warn, error
Your Valence API Key is required, while while all other fields are optional.

Usage

Install the package via npm.
npm install valenceai
You can validate the package configuration using the following code:
import { validateConfig } from 'valenceai';

try {
  validateConfig();
  console.log('Configuration is valid!');
} catch (error) {
  console.error('Configuration error:', error.message);
}
Client Constructor:
import { ValenceClient } from 'valenceai';

client = new ValenceClient(
    None,           // API key (or use VALENCE_API_KEY env var)
    5*1024*1024,    // Size of each upload chunk for asynch audio
    5              // Number of retry attempts
)
DiscreteAPI:
import { ValenceClient } from 'valenceai';

try {
  const client = new ValenceClient();
  const result = await client.discrete.emotions('YOUR_FILE.wav');
  console.log('Emotion detected:', result);
} catch (error) {
  console.error('Error:', error.message);
}
AsynchAPI:
import { ValenceClient } from 'valenceai';

try {
  const client = new ValenceClient();
  
  // Upload the audio file
  const requestId = await client.asynch.upload('YOUR_FILE.wav');
  console.log('Upload complete. Request ID:', requestId);
  
  // Get emotions from uploaded audio
  const emotions = await client.asynch.emotions(requestId);
  console.log('Emotions detected:', emotions);
} catch (error) {
  console.error('Error:', error.message);
}
Advanced AsynchAPI Usage: This usage is only recommended if you receive errors during file uploads or require smaller upload segments during the upload of a large file.
import { ValenceClient } from 'valenceai';

// Custom client configuration
const client = new ValenceClient(
  2 * 1024 * 1024,  // 2MB parts
  5                  // 5 retry attempts
);

// Upload with custom configuration
const requestId = await client.asynch.upload('huge_file.wav');

// Custom polling with more attempts and shorter intervals
const emotions = await client.asynch.emotions(
  requestId,
  50,    // 50 polling attempts
  3      // 3 second intervals
);
More information about additional parameters and expected values can be found in the API Reference.

Ready to Get Started?

Check out the Valence package on NPM →