# Getting Started

### Pre-requisites

* You have signed up for a Checkr account either directly with Checkr or through a partner.
* **Except for Sign-up & Connect embed**: You have implemented [Checkr OAuth](https://docs.checkr.com/partners/#retrieve-an-access-token) if you are a partner.


### To use a Checkr embed within your application

1. Load [Checkr's Web SDK](/embeds/getting-started#load-checkrs-web-sdk) directly through CDN. This ensures you always have the latest version.
2. Add the embed to your frontend code.
3. **Except for Sign-up & Connect embed**: Implement [authentication](/embeds/getting-started#add-authentication) for the embed using a SessionToken.
4. Optionally, customize the embed based on your application's needs.


## Load Checkr's Web SDK

First, Load Checkr's Web SDK using CDN

```html
<script src="https://cdn.jsdelivr.net/npm/@checkr/web-sdk/dist/web-sdk.umd.js"></script>
```

The [Examples](/embeds/examples) section below showcases some examples.

## Add authentication

For Sign-up & Connect embed no previous authentication is required. In fact, Sign-up & Connect embed will implement most of the [Checkr OAuth](https://docs.checkr.com/partners/#section/Getting-Started/Connect-your-customers-to-Checkr) flow for you. It provides your application with an [Oauth Authorization Code](https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow) to exchange for an [OAuth access token](https://docs.checkr.com/partners/#section/Getting-Started/Connect-your-customers-to-Checkr), which can be used by your application to make API calls to Checkr on behalf of your customers.

This Access Token must be used to request `SessionToken`s for authentication using other embeds.

### Notes on keeping your Access Token secure

Be aware that the following token exchanges **must take place within your own backend application** to avoid exposing Access Tokens in your frontend application, which are considered **sensitive secrets**:

1. Exchange of the Oauth Authorization Code for an Access Token
2. Exchange of the Access Token for a Session Token (specific to use with embeds.)


## Add authentication for other embeds

![Session Token Authentication](/assets/authentication-v3.3ee93cbf5e9e1b2fcfb580f97e591c01406e49f7112fad4aaa26fc671595e0a7.9c1bb791.png)

First, the embed authenticates with Checkr.

1. The embed sends a request from your application's frontend to your application's backend for a Checkr SessionToken.
2. Your application's backend must first authenticate the logged in user using its own mechanisms, and then pass the request to Checkr.
3. Checkr sends a SessionToken to your backend in response.
4. Your application's backend then passes the SessionToken to the embed running on your application's frontend.


While authentication is processing, the embed displays a loading state in your app. Then, the embed fetches and renders the returned data.

1. The embed uses Checkr APIs to request data.
2. Checkr APIs return the requested data directly to the embed.


When the requested data is returned, the embed renders within your application.

### Embed authentication flow

When an Embed loads for the first time, it sends an HTTP POST request to your backend, requesting a SessionToken. Use the `sessionTokenPath` property to set your application's backend endpoint to use.

By default, your application's cookies are sent to your backend to help you authenticate the user. To enable authentication via headers (such as bearer tokens), use the `sessionTokenRequestHeaders` property as shown below.

**Note:** `sessionTokenPath` is a path to request the token, not the token itself. You should not be passing the token as a prop directly to the embed.

JavaScript
```javascript
const embed = new Checkr.Embeds.NewInvitation({ sessionTokenPath: '/your-backend/session-tokens', sessionTokenRequestHeaders: () => ({ Authorization : `Bearer ${token}` })})
```

React
```jsx
<NewInvitation sessionTokenPath='/your-backend/checkr-session-tokens' sessionTokenRequestHeaders={() => ({ Authorization : `Bearer ${token}` })}/>
```

**Note:** Do not use a public endpoint for the `sessionTokenPath` property. Be certain to complete your application's user authentication and authorization before responding to the request.

Run your application's user authentication and authorization rules before requesting Checkr to acquire a SessionToken.

#### Step 1: The embed requests a SessionToken from your backend

The embed will use the `sessionTokenPath` property to request a SessionToken from your backend.

JavaScript
```javascript
const embed = new Checkr.Embeds.NewInvitation({ sessionTokenPath: '/your-backend/session-tokens' })
```

React
```jsx
<NewInvitation sessionTokenPath='/your-backend/checkr-session-tokens' />
```

#### Step 2: Send a request for a SessionToken from your backend to Checkr

Checkr provides two means to acquire this SessionToken: one for our direct customers, and one for our partner developers.

* **Customers building directly to the Checkr APIs:** Use the [API Keys](https://dashboard.checkr.com/account/developer_settings) found in the Checkr Dashboard to request SessionTokens.
* **Partner developers, building partner applications:** Use the OAuth Access Token acquired through [Checkr OAuth](https://docs.checkr.com/partners/#section/Getting-Started/Connect-your-customers-to-Checkr) to request SessionTokens. This is a pre-requisite for using Embeds. See the [Checkr Partner Guides](https://docs.checkr.com/partners) for more information.


When your application's backend receives the HTTP Post request from the embed, run authentication and authorization rules based on your application logic (for the current user), and then make the following call to Checkr.

Scopes are used to determine what access you are requesting for.

| Scope | Use case |
|  --- | --- |
| order | NewInvitation Embed, ReportsOverview Embed |
| disclosure | Disclosure & Consent Embed |


POST `{checkr-api-host}/web_sdk/session_tokens`

Checkr API host:

* Staging: https://api.checkr-staging.com/v1
* Production: https://api.checkr.com/v1


Use the following as the request JSON payload:

Partner Request
```shell
curl --request POST \
  --url {checkr-api-host}/web_sdk/session_tokens \
  --user your-checkr-oauth-access-token: \
  --header 'Content-Type: application/json' \
  --data '{
	"scopes": ["order"]
}'
```

Direct Customer Request
```shell
curl --request POST \
  --url {checkr-api-host}/web_sdk/session_tokens \
  --user your-checkr-api-key: \
  --header 'Content-Type: application/json' \
  --data '{
	"scopes": ["order"],
	"direct": true
}'
```

#### Step 3: Checkr responds to your backend with a SessionToken

Checkr will respond with a SessionToken.

```json
{
  "token": "example-session-token"
}
```

#### Step 4: Return the acquired SessionToken from your backend to your frontend

Return the JSON response (from above) from the Checkr API back to your frontend.

SessionTokens are short-lived. If they expire, the embed will automatically attempt to renew them by re-executing Steps 2 to 4.

## Use fakeMode to preview Embeds

Fake mode lets you play with the Embed without it making any API calls. When `fakeMode` is enabled in an embed (all embeds support it), the embed would work using canned data.

JavaScript
```javascript
const embed = new Checkr.Embeds.NewInvitation({ fakeMode: true })
```

React
```jsx
<NewInvitation fakeMode={true} />
```

## Use a Staging account to test Embeds

If you want to test with a [Checkr Staging account](https://docs.checkr.com/#section/Getting-Started/Request-a-Staging-Account), you can pass an additional param to any embed, `env: "staging"` to run the embeds against Staging. You can omit the `env` param entirely, or specify `env: "production"`, to use a production account.

**Note:** Embeds do not support mixed Staging and Production usage on the same page - ensure all concurrently rendered embeds are using the same environment.

JavaScript
```js
const embed = new Checkr.Embeds.SignUpFlow({
  env: 'staging'
  oauthTokenPath: '/your-backend/checkr-staging-oauth-token',
  partner: { id: 'abcdef1', name: 'Enterprise Partner Staging' },
});
```

React
```jsx
<SignUpFlow
  env='staging'
  oauthTokenPath='/your-backend/checkr-staging-oauth-token'
  partner={{ id: 'abcdef1', name: 'Enterprise Partner Staging' }}
/>
```

## Webhooks

While Embeds do not rely on [Webhooks](https://docs.checkr.com/#section/Webhooks), you can definitely use it in combination with Embeds to build more advanced features such as workflow automation in your product.