Skip to main content

Creating or fetching an Account

To provide a CTA for your customers, they'll have to be associated to a Nav Account. To do this you'll have to create an account with a userID of your choosing, this has to be a unique identifier created by you to link your user to our data. We recommend you use your user's ID/primary key in your database as the account userID.

Find or create an account

You can employ a find or create method that will check if an account exists and if not, create one right away.

Create a method that checks if the user exists.

import fetch from 'node-fetch'

const doesAccountExist = async (userID) => {
// Use GraphQL to request an Authentication Code
const response = await fetch('https://api.sandbox.nav.com/partners/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'my-secret-api-key',
'x-partner-id': 'my-partner-id',
},
body: JSON.stringify({
query: `
query CheckAccountExists($userID: String!) {
account(userID: $userID) {
userID
}
}
`,
variables: {
userID: userID,
},
}),
})
const responseData = await response.json()

// [account, doesTheAccountExist: true]
return [responseData.data.account, true]
}

Create a method that creates a user

import fetch from 'node-fetch'

// Sample accountInput variables
// {
// "userID": "user_q4ni4f8a",
// "email": "some_user@example.com",
// "businessName": "Casa Blanca Catering",
// "businessAddress": "1600 Pennsylvania Avenue",
// "businessCity": "Washington",
// "businessState": "DC",
// "businessZip": "20500",
// "businessPhone": "2024561111"
// }

const createAccount = async (accountInput) => {
// Use GraphQL to request an Authentication Code
const { userID, email, businessName, businessAddress, businessCity, businessState, businessZip, businessPhone } =
accountInput

const response = await fetch('https://api.sandbox.nav.com/partners/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'my-secret-api-key',
'x-partner-id': 'my-partner-id',
},
body: JSON.stringify({
query: `
mutation CreateNavAccount(
$userID: String!,
$email: String,
$businessName: String!,
$businessAddress: String,
$businessCity: String,
$businessState: String!,
$businessZip: String!,
$businessPhone: String,
) {
createAccount(
userID: $userID,
email: $email,
businessName: $businessName,
businessAddress: $businessAddress,
businessCity: $businessCity,
businessState: $businessState,
businessZip: $businessZip,
businessPhone: $businessPhone
) {
userID
email
businesses {
name
duns
experianBIN
equifaxID
}
}
}
`,
variables: {
userID,
email,
businessName,
businessAddress,
businessCity,
businessZip,
businessPhone,
},
}),
})
const responseData = await response.json()

return responseData.data.account
}

Bring it altogether and create our findOrCreate method

const findOrCreateAccount = async (userID, accountInfo) => {
const [account, accountExists] = await doesAccountExist(userID)

if (accountExists) {
return account
}

const newAccount = await createAccount({
userID,
...accountInfo,
})

return newAccount
}

References