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

# Create a token

POST https://api.notion.com/v1/oauth/token
Content-Type: application/json

For step-by-step instructions on how to use this endpoint to create a public integration, check out the [Authorization guide](/docs/authorization#set-up-the-auth-flow-for-a-public-integration). To walkthrough how to create tokens for Link Previews, refer to the [Link Previews guide](/docs/build-a-link-preview-integration).

The `redirect_uri` is a *required* field in the request body for this endpoint if:

* the `redirect_uri` query parameter was set in the [Authorization URL](/docs/authorization#step-1-navigate-the-user-to-the-integrations-authorization-url) provided to users, *or*;
* there are more than one `redirect_uri`s included in the [integration’s settings](https://www.notion.so/my-integrations) under **OAuth Domain & URIs**.

In most cases, the `redirect_uri` field is required.

This field is not allowed in the request body if:

* there is one `redirect_uri` included in the [integration’s settings](https://www.notion.so/my-integrations) under **OAuth Domain & URIs**, *and* the `redirect_uri` query parameter was not included in the Authorization URL.

Learn more in the public integration section of the [Authorization Guide](/docs/authorization#public-integration-auth-flow-set-up).

*Note: Each Public API endpoint can return several possible error codes. To see a full description of each type of error code, see the [Error codes section](/reference/status-codes#error-codes) of the Status codes documentation.*

Reference: https://notion.ferndocs.com/authentication/create-a-token

## Request

### Headers

- `Authorization` (string, optional, default: Basic $BASE64_ENCODED_ID_AND_SECRET)

### Body (application/json)

This endpoint expects an object.

- `code` (string, required) — A unique random code that Notion generates to authenticate with your service, generated when a user initiates the OAuth flow.
- `grant_type` (string, required) — A constant string: "authorization_code"
- `redirect_uri` (string, required) — The "redirect_uri" that was provided in the OAuth Domain & URI section of the integration's Authorization settings. Do not include this field if a "redirect_uri" query param was not included in the Authorization URL provided to users. In most cases, this field is required.

## Response

### 200

200

- `access_token` (string, optional)
- `refresh_token` (string, optional)
- `bot_id` (string, optional)
- `duplicated_template_id` (any, optional)
- `owner` (V1OauthTokenPostResponsesContentApplicationJsonSchemaOwner, optional)
- `workspace_icon` (string, optional)
- `workspace_id` (string, optional)
- `workspace_name` (string, optional)

## Errors

### 400 Bad Request Error

400

- `error` (string, optional)
- `error_description` (string, optional)

## Types

### V1OauthTokenPostResponsesContentApplicationJsonSchemaOwner

- `workspace` (boolean, optional, default: true)

## Examples

**Request**

```json
{
  "code": "string",
  "grant_type": "string",
  "redirect_uri": "string"
}
```

**Response**

```json
{
  "access_token": "e202e8c9-0990-40af-855f-ff8f872b1ec6c",
  "bot_id": "b3414d659-1224-5ty7-6ffr-cc9d8773drt601288f",
  "duplicated_template_id": {},
  "owner": {
    "workspace": true
  },
  "workspace_icon": "https://website.domain/images/image.png",
  "workspace_id": "j565j4d7x3-2882-61bs-564a-jj9d9ui-c36hxfr7x",
  "workspace_name": "Ada's Notion Workspace"
}
```

**SDK Code**

```python
import requests

url = "https://api.notion.com/v1/oauth/token"

payload = {
    "code": "string",
    "grant_type": "string",
    "redirect_uri": "string"
}
headers = {"Content-Type": "application/json"}

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

print(response.json())
```

```javascript
const url = 'https://api.notion.com/v1/oauth/token';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"code":"string","grant_type":"string","redirect_uri":"string"}'
};

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

```go
package main

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

func main() {

	url := "https://api.notion.com/v1/oauth/token"

	payload := strings.NewReader("{\n  \"code\": \"string\",\n  \"grant_type\": \"string\",\n  \"redirect_uri\": \"string\"\n}")

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

	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
require 'uri'
require 'net/http'

url = URI("https://api.notion.com/v1/oauth/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.body = "{\n  \"code\": \"string\",\n  \"grant_type\": \"string\",\n  \"redirect_uri\": \"string\"\n}"

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

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

HttpResponse<String> response = Unirest.post("https://api.notion.com/v1/oauth/token")
  .header("Content-Type", "application/json")
  .body("{\n  \"code\": \"string\",\n  \"grant_type\": \"string\",\n  \"redirect_uri\": \"string\"\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.notion.com/v1/oauth/token', [
  'body' => '{
  "code": "string",
  "grant_type": "string",
  "redirect_uri": "string"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp
using RestSharp;

var client = new RestClient("https://api.notion.com/v1/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"code\": \"string\",\n  \"grant_type\": \"string\",\n  \"redirect_uri\": \"string\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "code": "string",
  "grant_type": "string",
  "redirect_uri": "string"
] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "https://api.notion.com/v1/oauth/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()
```