---
title: "Linking an Account"
---
To securely authenticate an end user's connection to an accounting platform you will need to:
<CardGroup cols={2}>
  <Card title="Initiate Access" icon="square-1" href="/account_link#step-1-initiate-access">
    Get a token to access Strada Link for your end customer
  </Card>
  <Card title="Render Component" icon="square-2" href="/account_link#step-2-render-component">
    Initialize and render Strada Link component in your app
  </Card>
  <Card title="Get Public Token" icon="square-3" href="/account_link#step-3-get-public-token">
    Receive a public token after your user successfully authenticates
  </Card>
  <Card title="Swap Token" icon="square-4" href="/account_link#step-4-swap-token">
    Exchange the public token for a private one
  </Card>
</CardGroup>

![Strada Link Flow](/public/get-started.svg "Strada Link Flow")


## Step 1. Initiate Access

In your backend service, create a `POST` request to get a `link_access_token`. See the `POST` request body and API response [here](/api-reference/link/create-a-link-access-token).

<Note> `YOUR-API-KEY` is provided during onboarding. It is a UUID prefixed by the environment (`prod_*` or `sandbox_*`)</Note>

<Tip>For Sandbox, the base URL is  *sandbox.api.getstrada.com*</Tip>

<CodeGroup>


```javascript JavaScript
const url = 'https://api.getstrada.com/v1/link/link_access_token';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json', Authorization: 'Bearer <YOUR-API-KEY>'},
  body: '{"end_customer_ref_id":"panam","end_customer_organization_name":"Pan American Airlines Inc.","end_customer_email":"frank@panam.com"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```python Python
import requests

url = "https://api.getstrada.com/v1/link/link_access_token"

payload = {
    "end_customer_ref_id": "panam",
    "end_customer_organization_name": "Pan American Airlines Inc.",
    "end_customer_email": "frank@panam.com"
}
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer <YOUR-API-KEY>"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```ruby Ruby
require 'uri'
require 'net/http'

url = URI("https://api.getstrada.com/v1/link/link_access_token")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request["Authorization"] = 'Bearer <YOUR-API-KEY>'
request.body = "{\n  \"end_customer_ref_id\": \"panam\",\n  \"end_customer_organization_name\": \"Pan American Airlines Inc.\",\n  \"end_customer_email\": \"frank@panam.com\"\n}"

response = http.request(request)
puts response.read_body
```

```java Java
AsyncHttpClient client = new DefaultAsyncHttpClient();
client.prepare("POST", "https://api.getstrada.com/v1/link/link_access_token")
  .setHeader("Content-Type", "application/json")
  .setHeader("Authorization", "Bearer <YOUR-API-KEY>")
  .setBody("{\n  \"end_customer_ref_id\": \"panam\",\n  \"end_customer_organization_name\": \"Pan American Airlines Inc.\",\n  \"end_customer_email\": \"frank@panam.com\"\n}")
  .execute()
  .toCompletableFuture()
  .thenAccept(System.out::println)
  .join();

client.close();
```

```go Go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.getstrada.com/v1/link/link_access_token"

	payload := strings.NewReader("{\n  \"end_customer_ref_id\": \"panam\",\n  \"end_customer_organization_name\": \"Pan American Airlines Inc.\",\n  \"end_customer_email\": \"frank@panam.com\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer <YOUR-API-KEY>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```shell Shell
curl --request POST \
  --url https://api.getstrada.com/v1/link/link_access_token \
  --header 'Authorization: Bearer <YOUR-API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "end_customer_ref_id": "panam",
  "end_customer_organization_name": "Pan American Airlines Inc.",
  "end_customer_email": "frank@panam.com"
}'
```
</CodeGroup>



## Step 2. Render Component 

Strada Link is published to [npm](https://www.npmjs.com/package/@stradahq/strada-link-react) as a React library that you can invoke in your frontend application. The source code can be found [here](https://github.com/strada-api/strada-link-react). 

Install the library:
```shell
npm i @stradahq/strada-link-react@latest
```

Import `useStradaLink` where you want the component to appear: 

```typescript
import { useStradaLink } from '@stradahq/strada-link-react'
```

Initialize `useStradaLink` with the following required parameters:
    - `env` - (one of `sandbox`, `prod`).
    - `linkAccessToken` - access token retrieved from the [previous step](/account_link#step-1-initiate-access).
    - `onSuccess` - call back function that will be called on a successful account link.

You can view a fully working react app using `strada-link-react` [here](https://github.com/strada-api/strada-link-example). A simple app would be:
```typescript 
import { useCallback, useState } from "react";
import { useStradaLink } from "@stradahq/strada-link-react";

function App() {
  const [publicToken, setPublicToken] = useState<string | null>(null);
  const onSuccess = useCallback((public_token: string) => {
    setPublicToken(public_token);
  }, []);

  const { open, isReady } = useStradaLink({
    env: "prod",
    linkAccessToken: "ACCESS_TOKEN_FROM_STEP_1",
    onSuccess,
  });

  if (publicToken) {
    return <h1>{publicToken}</h1>;
  } else {
    return (
      <div>
        <button disabled={!isReady} onClick={open}>
          Connect Account
        </button>
      </div>
    );
  }
}

export default App;
```

## Step 3. Get Public Token 

If authentication is successful, the `onSuccess` method will be invoked with the `public_connection_token` as a parameter. 

Pass the `public_connection_token` to your backend to [swap for a private token](/account_link#step-4-swap-token).

## Step 4. Swap Token 

In your backend, swap `public_connection_token` for a private `api_connection_token`. See the `POST` request body and API response [here](/api-reference/link/get-api-connection-token).

<Note>
Store the `api_connection_token` in your database as it will be used to authenticate future accounting API requests for this given end user. You cannot make requests via the `public_connection_token`.
</Note>

<Tip>For Sandbox, the base URL is  *sandbox.api.getstrada.com*</Tip>

<CodeGroup>
```javascript JavaScript
const url = 'https://sandbox.api.getstrada.com/v1/link/api_connection_token';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json', Authorization: 'Bearer <YOUR-API-KEY>'},
  body: '{"public_connection_token":"pub_123"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```python Python
import requests

url = "https://sandbox.api.getstrada.com/v1/link/api_connection_token"

payload = { "public_connection_token": "pub_123" }
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer <YOUR-API-KEY>"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```ruby Ruby
require 'uri'
require 'net/http'

url = URI("https://sandbox.api.getstrada.com/v1/link/api_connection_token")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request["Authorization"] = 'Bearer <YOUR-API-KEY>'
request.body = "{\n  \"public_connection_token\": \"pub_123\"\n}"

response = http.request(request)
puts response.read_body
```

```java Java
AsyncHttpClient client = new DefaultAsyncHttpClient();
client.prepare("POST", "https://sandbox.api.getstrada.com/v1/link/api_connection_token")
  .setHeader("Content-Type", "application/json")
  .setHeader("Authorization", "Bearer <YOUR-API-KEY>")
  .setBody("{\n  \"public_connection_token\": \"pub_123\"\n}")
  .execute()
  .toCompletableFuture()
  .thenAccept(System.out::println)
  .join();

client.close();
```

```go Go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://sandbox.api.getstrada.com/v1/link/api_connection_token"

	payload := strings.NewReader("{\n  \"public_connection_token\": \"pub_123\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer <YOUR-API-KEY>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```shell Shell
curl --request POST \
  --url https://sandbox.api.getstrada.com/v1/link/api_connection_token \
  --header 'Authorization: Bearer <YOUR-API-KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "public_connection_token": "pub_123"
}'
```
</CodeGroup>