API Reference Overview
Complete reference for the ToughClicks JavaScript SDK.
Constructor
new ToughClicks(
token: string,
packetIds: string | string[],
debug?: boolean,
environment?: TCEnvironment,
packetDisplayOptions?: TCPacketDisplayOptions[],
getPageContext?: () => TCContextState
)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | Yes | Your client authentication token |
packetIds | string | string[] | Yes | Single packet ID or array of packet IDs |
debug | boolean | No | Enable debug logging (default: false) |
environment | TCEnvironment | No | Environment ('production' or 'staging', default: 'production') |
packetDisplayOptions | TCPacketDisplayOptions[] | No | Array of display options per packet |
getPageContext | () => TCContextState | No | Custom context provider function |
Example
const tc = new ToughClicks(
'your-token',
['packet1', 'packet2'],
true,
'production',
[/* display options */]
);
Core Methods
render()
Render all configured packets.
render(): Promise<boolean>
Returns: Promise<boolean> - true if rendering succeeded
Example:
const success = await tc.render();
if (success) {
console.log('Packets rendered successfully');
}
accept()
Manually trigger acceptance for all packets.
accept(): Promise<boolean>
Returns: Promise<boolean> - true if acceptance succeeded
Example:
const accepted = await tc.accept();
if (accepted) {
console.log('All packets accepted');
}
verify()
Verify user compliance across all packets.
verify(userId: string): Promise<TCVerificationResponse[]>
Parameters:
userId- The user identifier to verify
Returns: Promise<TCVerificationResponse[]> - Array of verification results, one per packet
If a packet can't be verified because the API is unreachable or returns an error, its result has userCompliant: true and verificationFailed: true (fail-open), and tc-internal-error is emitted. See TCVerificationResponse.
Example:
const results = await tc.verify('user@example.com');
results.forEach(result => {
if (result.verificationFailed) {
console.warn(`Packet ${result.id}: could not verify`);
} else {
console.log(`Packet ${result.id}: ${result.userCompliant}`);
}
});
Display Options Methods
setPacketDisplayOptions()
Set display options for a specific packet.
setPacketDisplayOptions(
packetId: string,
options: TCDisplayOptions
): boolean
Parameters:
packetId- The packet ID to configureoptions- Display options object
Returns: boolean - true if options were set successfully
Example:
tc.setPacketDisplayOptions('packet1', {
containerId: 'container1',
signerIdSelector: 'email-input',
displayMethod: TCDisplayMethod.individual,
shouldOpenModal: true,
manualAcceptance: false
});
getPacketDisplayOptions()
Get display options for a specific packet.
getPacketDisplayOptions(packetId: string): TCDisplayOptions | null
Parameters:
packetId- The packet ID to query
Returns: TCDisplayOptions | null - Display options or null if not found
Example:
const options = tc.getPacketDisplayOptions('packet1');
console.log(options.displayMethod);
setAllPacketDisplayOptions()
Set display options for multiple packets at once.
setAllPacketDisplayOptions(
packetDisplayOptions: TCPacketDisplayOptions[]
): boolean
Parameters:
packetDisplayOptions- Array of packet display configurations
Returns: boolean - true if options were set successfully
Example:
tc.setAllPacketDisplayOptions([
{
packetId: 'packet1',
displayOptions: { /* options */ }
},
{
packetId: 'packet2',
displayOptions: { /* options */ }
}
]);
setDisplayOptions() (Legacy)
Set display options for single-packet usage.
setDisplayOptions(options: TCDisplayOptions): boolean
Note: For backwards compatibility. Use setPacketDisplayOptions() for multi-packet support.
State Management Methods
setCustomData()
Set custom data for all packets.
setCustomData(customData: object): boolean
Parameters:
customData- Object containing custom metadata
Returns: boolean - true if set successfully
Example:
tc.setCustomData({
orderId: '12345',
subscriptionTier: 'premium',
referralSource: 'email-campaign'
});
setSignerEmailAddress()
Set the signer's email address for all packets.
setSignerEmailAddress(email: string): boolean
Parameters:
email- Email address of the signer
Returns: boolean - true if set successfully
Example:
tc.setSignerEmailAddress('user@example.com');
setDynamicData()
Set dynamic data for document rendering.
setDynamicData(dynamicData: object | null): boolean
Parameters:
dynamicData- Object containing dynamic values or null to clear
Returns: boolean - true if set successfully
Example:
tc.setDynamicData({
userName: 'John Doe',
accountType: 'Premium',
expirationDate: '2025-12-31'
});
Utility Methods
setDebug()
Enable or disable debug mode.
setDebug(debug: boolean): boolean
Parameters:
debug-trueto enable,falseto disable
Returns: boolean - true if set successfully
Example:
tc.setDebug(true); // Enable debug logging
setTTL()
Set cache time-to-live in milliseconds.
setTTL(ttl: number): boolean
Parameters:
ttl- Time-to-live in milliseconds
Returns: boolean - true if set successfully
Example:
tc.setTTL(3600000); // 1 hour cache
clearCache()
Clear all cached data.
clearCache(): boolean
Returns: boolean - true if cache was cleared
Example:
tc.clearCache();
Event Handling
on()
Register an event listener.
on(event: TCEventTypes, callback: Function): void
Parameters:
event- Event type ('tc-valid', 'tc-accepted', 'tc-internal-error')callback- Function to call when event fires
Example:
tc.on('tc-valid', (state) => {
console.log('Validation changed:', state.allPacketsValid);
});
tc.on('tc-accepted', (state) => {
console.log('Accepted:', state.allPacketsAccepted);
});
tc.on('tc-internal-error', (error) => {
console.error('Error:', error);
});
Next Steps
- Explore Type Definitions
- Learn about Events
- See complete JavaScript Library documentation