> This page is for Agent Handler.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.merge.dev/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.merge.dev/_mcp/server.

# Create Link token

POST https://ah-api.merge.dev/api/v1/registered-users/{registered_user_id}/link-token/
Content-Type: application/json

Generates a Link Token for a Registered User to authenticate with Connectors. Also returns a magic link URL that can be shared with the user to authenticate in a browser.

Reference: https://docs.merge.dev/merge-agent-handler/agent-handler/link-token/create-for-registered-user

## Authentication

- `Authorization` header (bearer token, required) — Token-based authentication with required prefix "Bearer"

## Request

### Path parameters

- `registered_user_id` (string, required) — Agent Handler ID of the Registered User

### Body (application/json)

This endpoint expects an object.

- `connector` (string, required) — Slug of the connector to generate a link token for
- `external_id` (string, optional, nullable) — Optional external ID to scope the link token to a specific application credential. Use this when you have multiple OAuth apps for the same connector.
- `callback_url` (string, optional, nullable) — Optional URL to redirect the user to after they complete the authentication flow. Must be added to your organization's allowed callback origins first.
- `state` (string, optional, nullable) — Optional CSRF protection token returned to your callback_url after authentication. Requires callback_url to be set. Do not include a 'state' query parameter in callback_url if using this field.

## Response

### 201

- `link_token` (string, optional)
- `magic_link_url` (string, optional) — URL that opens a hosted authentication page. Share this with the user so they can connect their account in a browser.

## Examples

### Basic request

**Request**

```json
{
  "connector": "linear"
}
```

**Response**

```json
{
  "link_token": "string",
  "magic_link_url": "string"
}
```

**SDK Code**

```python Basic request
import requests

url = "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/"

payload = { "connector": "linear" }
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

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

print(response.json())
```

```javascript Basic request
const url = 'https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"connector":"linear"}'
};

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

```go Basic request
package main

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

func main() {

	url := "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/"

	payload := strings.NewReader("{\n  \"connector\": \"linear\"\n}")

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

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

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

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

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

}
```

```ruby Basic request
require 'uri'
require 'net/http'

url = URI("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"connector\": \"linear\"\n}"

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

```java Basic request
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"connector\": \"linear\"\n}")
  .asString();
```

```php Basic request
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/', [
  'body' => '{
  "connector": "linear"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp Basic request
using RestSharp;

var client = new RestClient("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"connector\": \"linear\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Basic request
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = ["connector": "linear"] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```

### Request with external_id and callback_url

**Request**

```json
{
  "connector": "linear",
  "external_id": "acme-corp",
  "callback_url": "https://your-app.com/auth/callback",
  "state": "random-csrf-token"
}
```

**Response**

```json
{
  "link_token": "string",
  "magic_link_url": "string"
}
```

**SDK Code**

```python Request with external_id and callback_url
import requests

url = "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/"

payload = {
    "connector": "linear",
    "external_id": "acme-corp",
    "callback_url": "https://your-app.com/auth/callback",
    "state": "random-csrf-token"
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

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

print(response.json())
```

```javascript Request with external_id and callback_url
const url = 'https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"connector":"linear","external_id":"acme-corp","callback_url":"https://your-app.com/auth/callback","state":"random-csrf-token"}'
};

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

```go Request with external_id and callback_url
package main

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

func main() {

	url := "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/"

	payload := strings.NewReader("{\n  \"connector\": \"linear\",\n  \"external_id\": \"acme-corp\",\n  \"callback_url\": \"https://your-app.com/auth/callback\",\n  \"state\": \"random-csrf-token\"\n}")

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

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

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

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

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

}
```

```ruby Request with external_id and callback_url
require 'uri'
require 'net/http'

url = URI("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"connector\": \"linear\",\n  \"external_id\": \"acme-corp\",\n  \"callback_url\": \"https://your-app.com/auth/callback\",\n  \"state\": \"random-csrf-token\"\n}"

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

```java Request with external_id and callback_url
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"connector\": \"linear\",\n  \"external_id\": \"acme-corp\",\n  \"callback_url\": \"https://your-app.com/auth/callback\",\n  \"state\": \"random-csrf-token\"\n}")
  .asString();
```

```php Request with external_id and callback_url
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/', [
  'body' => '{
  "connector": "linear",
  "external_id": "acme-corp",
  "callback_url": "https://your-app.com/auth/callback",
  "state": "random-csrf-token"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp Request with external_id and callback_url
using RestSharp;

var client = new RestClient("https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"connector\": \"linear\",\n  \"external_id\": \"acme-corp\",\n  \"callback_url\": \"https://your-app.com/auth/callback\",\n  \"state\": \"random-csrf-token\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Request with external_id and callback_url
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "connector": "linear",
  "external_id": "acme-corp",
  "callback_url": "https://your-app.com/auth/callback",
  "state": "random-csrf-token"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://ah-api.merge.dev/api/v1/registered-users/41f11808-5387-4be5-9af2-852a372d61e3/link-token/")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```