Skip to main content

Complete Customer Workflow

A complete example showing the full customer lifecycle: creating a customer, generating a bearer token, creating a handbook, and retrieving it.

Step 1: Create Customer

mutation CreateHandbookCustomer($input: CustomerInput!) {
createHandbookCustomer(input: $input) {
userId
email
name
createdAt
customerHandbookLimit
}
}

Variables:

{
"input": {
"email": "[email protected]",
"name": "John Doe",
"userId": "external_user_123",
"handbookLimit": 10
}
}

Step 2: Generate Bearer Token

mutation GenerateToken($userId: String) {
generateHandbookCustomerBearerToken(userId: $userId) {
token
issuedAt
expiresAt
expiresIn
}
}

Variables:

{
"userId": "external_user_123"
}

Step 3: Generate Handbook (Using Bearer Token)

mutation GenerateHandbook($input: HandbookRequest!, $subscriptionType: SubscriptionType!) {
generateHandbook(input: $input, subscriptionType: $subscriptionType) {
content
contentType
subscriptionType
}
}

Variables:

{
"input": {
"companyName": "Acme Corporation",
"myState": "CA",
"numberOfEmployees": 50,
"multiState": false,
"industry": "OTHER",
"fullTimeHours": 40,
"payPeriodFrequency": "WEEKLY",
"trialPeriod": true,
"trialPeriodDays": 90,
"paidHolidays": true,
"vacationBenefits": true,
"sickLeave": true,
"healthInsurance": true,
"directDeposit": true
},
"subscriptionType": "ONE_TIME"
}

Note: Use the bearer token from Step 2 in the Authorization header with the Customer API endpoint (/api/graphql-public).

Step 4: List Handbooks

query ListHandbooks {
listHandbooks {
handbooks {
handbookId
handbookType
createdAt
companyName
industry
}
total
}
}

Step 5: Get Specific Handbook

query GetHandbook($handbookId: Int!) {
getHandbook(handbookId: $handbookId) {
handbookId
content
createdAt
data {
companyName
myState
industry
}
}
}

Variables:

{
"handbookId": 123
}

Complete JavaScript Workflow

const API_TOKEN = 'your_api_token';
const RESELLER_ENDPOINT = 'https://handbooks.io/api/graphql-reseller';
const CUSTOMER_ENDPOINT = 'https://handbooks.io/api/graphql-public';

// Step 1: Create Customer
async function createCustomer(email, name, userId) {
const response = await fetch(RESELLER_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Token': API_TOKEN,
},
body: JSON.stringify({
query: `
mutation($input: CustomerInput!) {
createHandbookCustomer(input: $input) {
userId
email
name
createdAt
}
}
`,
variables: {
input: { email, name, userId, handbookLimit: 10 },
},
}),
});
return (await response.json()).data.createHandbookCustomer;
}

// Step 2: Generate Bearer Token
async function generateBearerToken(userId) {
const response = await fetch(RESELLER_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Token': API_TOKEN,
},
body: JSON.stringify({
query: `
mutation($userId: String) {
generateHandbookCustomerBearerToken(userId: $userId) {
token
expiresAt
}
}
`,
variables: { userId },
}),
});
return (await response.json()).data.generateHandbookCustomerBearerToken.token;
}

// Step 3: Generate Handbook (using Customer API)
async function generateHandbook(bearerToken, handbookInput) {
const response = await fetch(CUSTOMER_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${bearerToken}`,
},
body: JSON.stringify({
query: `
mutation($input: HandbookRequest!, $subscriptionType: SubscriptionType!) {
generateHandbook(input: $input, subscriptionType: $subscriptionType) {
content
contentType
subscriptionType
}
}
`,
variables: { input: handbookInput, subscriptionType: 'ONE_TIME' },
}),
});
return (await response.json()).data.generateHandbook;
}

// Complete Workflow
async function completeWorkflow() {
try {
// 1. Create customer
const customer = await createCustomer(
'[email protected]',
'John Doe',
'external_user_123'
);
console.log('Customer created:', customer);

// 2. Generate bearer token
const bearerToken = await generateBearerToken(customer.userId);
console.log('Bearer token generated');

// 3. Generate handbook
const handbook = await generateHandbook(bearerToken, {
companyName: 'Acme Corporation',
myState: 'CA',
numberOfEmployees: 50,
multiState: false,
multiStateSelectedStates: [],
industry: 'OTHER',
fullTimeHours: 40,
payPeriodFrequency: 'WEEKLY',
trialPeriod: true,
trialPeriodDays: 90,
paidHolidays: true,
vacationBenefits: true,
sickLeave: true,
healthInsurance: true,
directDeposit: true,
});
console.log('Handbook generated', handbook);
} catch (error) {
console.error('Error in workflow:', error);
}
}

// Run the workflow
completeWorkflow();

Reseller Workflow Alternative

For reseller operations, you can skip steps 2 and generate handbooks directly:

// Generate handbook as reseller
async function generateHandbookReseller(email, handbookInput, subscriptionType) {
const response = await fetch(RESELLER_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Token': API_TOKEN,
},
body: JSON.stringify({
query: `
mutation($email: String, $input: HandbookRequest!, $subscriptionType: SubscriptionType!) {
generateHandbook(email: $email, input: $input, subscriptionType: $subscriptionType) {
content
contentType
subscriptionType
}
}
`,
variables: { email, input: handbookInput, subscriptionType },
}),
});
return (await response.json()).data.generateHandbook;
}