Quick Start
1. Injecting the script
To install the SDK, you need to add a script
tag at the end of your HTML file using one of the distribution channels. There are two main ways to do this:
- Directly via HTML:
<!-- Add inside the body after all the code -->
<script src="https://chatbot-api.handtalk.me/api/1.0.0/ht-api-sdk.min.js"></script>
- Using JavaScript:
const script = document.createElement("script");
script.src = "https://chatbot-api.handtalk.me/api/1.0.0/ht-api-sdk.min.js";
document.body.appendChild(script);
Regardless of the method, once the script is loaded, the HTApi
class will be available in the global window
object.
Tip: Wait for the script to load
If you try to instantiate the HTApi
class immediately after inserting the script
tag, you might get an error indicating that the class is not available. This happens because the script loading is not synchronous. To fix this, we recommend using the second script injection method and using the onload
function to wait for the script to finish loading, ensuring that the HTApi
class is available.
const script = document.createElement("script");
script.src = "https://chatbot-api.handtalk.me/api/1.0.0/ht-api-sdk.min.js";
document.body.appendChild(script);
script.onload = () => {
// Initialize the library here
};
2. Instantiating the class
Now that the library has been loaded into your code, you just need to instantiate the HTApi
class and use its methods. In the code below, we will instantiate HTApi
with the minimum required information, start the library, and perform a simple translation:
const htapi = new window.HTApi({ token: "YOUR TOKEN" });
htapi.active();
// Simulate library loading time
setTimeout(() => {
htapi.translate("Hello world!");
}, 2000);
3. Complete example
Combining all the code snippets, we have something like the example below:
const script = document.createElement("script");
script.src = "https://chatbot-api.handtalk.me/api/1.0.0/ht-api-sdk.min.js";
document.body.appendChild(script);
script.onload = () => {
const htapi = new window.HTApi({ token: "YOUR TOKEN" });
htapi.active();
// Simulate library loading time
setTimeout(() => {
htapi.translate("Hello world!");
}, 2000);
};