This code is a basic example of a prospect sign up page using the signUpProspect mutation built in simple HTML and pure JavaScript.
The URL used in the fetch request will depend on which regional based API you wish to query. Please replace {REGION} with either eu to query our EU based public API or na to query our North America based public API.
<html>
<head>
<title>Human Risk Report - Sign Up Example</title>
<style>
html,
body {
font-size: 15px;
}
.create-prospect>div {
margin-bottom: 10px;
}
.create-prospect label {
display: block;
}
.create-prospect input {
width: 200px;
}
</style>
</head>
<body>
<div>
<form class="create-prospect">
<div><strong><label for="email">Email</label></strong> <input id="email" autocomplete="username" name="email"
required="" type="email" placeholder="[email protected]"></div>
<div><strong><label for="name">Company name</label></strong> <input id="name" name="name" required="" type="text"
placeholder="..."></div>
<div><strong><label for="domain">Domain</label></strong> <input id="domain" name="domain" required="" type="text"
placeholder="company.com"></div>
<button type="submit">Sign up</button>
</form>
</div>
<script src="https://unpkg.com/unfetch/polyfill"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script>
var form = document.querySelector('.create-prospect')
var errorContainer = document.querySelector('.errors')
// This is the GraphQL mutation to create a prospect
var signUpProspectMutation = 'mutation ($name: String, $domain: String, $email: String!) {signUpProspect(name: $name, domain: $domain, email: $email) { id name }}'
var getValue = function getValue(name) {
return form.elements[name] ? form.elements[name].value : null
}
var signUp = function signUp(ev) {
ev.preventDefault()
// This is the live API URL so any prospects you create will appear in uService
fetch(
'https://public-api.{REGION}.usecure.io', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-api-key': 'YOUR CLIENT API KEY' // The Client API Key you generated in the system settings
},
body: JSON.stringify({
query: signUpProspectMutation,
variables: {
name: getValue('name'),
email: getValue('email'),
domain: getValue('domain')
},
}),
}).then(function (resp) {
return resp.json()
}).then(function (result) {
var errors = result.errors
if (errors) {
// Example for pulling the error code and message from the error response and displaying it in an alert
// _.get is from lodash
var errorMessage = _.get(errors, '[0].message') || 'An error occurred signing up for the human risk report'
var errorCode = _.get(errors, '[0].extensions.exception.errorCode')
if (errorCode) {
errorMessage += ' [' + errorCode + ']'
}
alert(errorMessage)
return false
} else {
alert('Thank you for signing up. You\'ve taken your first step into a larger world!')
return true
}
})
}
form.addEventListener('submit', signUp)
</script>
</body>
</html>
The code in this example takes a company name, company domain and email address as that is the full dataset signUpProspect mutation requires. However you can use only an email address without the name and domain. The mutation will derive these values from the email address.
The example above includes minimal error handling which you can extend as you wish.
The signUpProspect mutation can fail for following reasons:
Please bare these issues in mind when adding validation and error handling to your code.
The signUpProspect mutation returns an error response if the above validation requirements are not met. The status code can be 200 so you will always need to check the response errors. The sample code includes an example of how to process this using the lodash library.
This is an example of our error response i.e. an Apollo Server error response using a proprietary exception class:
{
"errors": [
{
"message": "This company domain has already been used.",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"signUpProspect"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"errorCode": "uniqueDomainError",
"isUsecure": true
}
}
}
],
"data": {
"signUpProspect": null
}
}
You can use our error message by pulling errors[0].message or use the error code to detect an error type using errors[0].extensions.exception.errorCode and provide your own message copy.