Getting started with Svelte

1. Install and import the @botpoison/browser library.
npm install @botpoison/browser
2. Create a new Botpoison instance with your public key.
3. Process a challenge using the challenge() function.
4. Forward the solution to your server for verification.
markup
<script>
import { onMount } from 'svelte'
import axios from "axios";
// 1. Import the library
import Botpoison from "@botpoison/browser";
let botpoison = null;
let message = null;
let loading = false;
onMount(() => {
// 2. Create a new instance with your public key
botpoison = new Botpoison({
publicKey: "pk_xxxxxxxx",
});
});
async function onSubmit() {
loading = true;
try {
// 3. Process a challenge
const { solution } = await botpoison.challenge();
await axios.post("https://example.demo/", {
message: message,
// 4. Forward the solution
_botpoison: solution,
});
alert("Form submitted");
message = null;
} catch (error) {
alert("Error submitting form");
} finally {
loading = false;
}
}
</script>
<form on:submit|preventDefault="{onSubmit}">
<textarea bind:value="{message}" />
<button type="submit" disabled="{loading}">Send</button>
</form>