Package Exports
- swifty-sdk
Readme
Swifty SDK
A lightweight SDK for integrating Swifty into your web application.
Installation
npm install swifty-sdk
Quick Start
import Swifty from 'swifty-sdk';
// Initialize the SDK
const swifty = new Swifty({ apiKey: 'your-api-key' });
// Load the widget into a container
await swifty.loadWidget({
selector: '#widget-container',
message: 'Welcome!'
});
API Reference
Constructor
Creates a new instance of the Swifty SDK.
const swifty = new Swifty({ apiKey: 'your-api-key' });
Methods
loadWidget(options)
Loads the Swifty widget into a specified container element.
const success = await swifty.loadWidget({
selector: '#widget-container', // CSS selector for the container
message: 'Welcome!' // Optional welcome message
});
Returns a Promise that resolves to true
if the widget loads successfully, false
otherwise.
open(options)
Opens the Swifty widget either in the current window or a new window.
// Open in current window
swifty.open();
// Open in new window with a message
swifty.open({
newWindow: true,
message: 'Hello!'
});
recognizeSpeech()
Activates speech recognition and returns the transcript. It uses the language set on html attribute or configured in the agent settings.
try {
// Recognize speech and use the transcript to load the widget
const transcript = await swifty.recognizeSpeech();
await swifty.loadWidget({
selector: '#widget-container',
message: transcript
});
} catch (error) {
console.error('Speech recognition failed:', error);
}
Returns a Promise that resolves to the recognized speech text or rejects with a SpeechRecognitionError
.
Browser Support
- Chrome 33+
- Edge 79+
- Safari 14+
- Firefox (all features except speech recognition)
Speech recognition functionality is currently not supported in Firefox.
License
MIT