Skip to content

Client

Synchronous and asynchronous clients for Notion's API.

AsyncClient

Bases: BaseClient

Asynchronous client for Notion's API.

Source code in notion_client/client.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
class AsyncClient(BaseClient):
    """Asynchronous client for Notion's API."""

    client: httpx.AsyncClient

    def __init__(
        self,
        options: Optional[Union[Dict[str, Any], ClientOptions]] = None,
        client: Optional[httpx.AsyncClient] = None,
        **kwargs: Any,
    ) -> None:
        if client is None:
            client = httpx.AsyncClient()
        super().__init__(client, options, **kwargs)

    async def __aenter__(self) -> "AsyncClient":
        self.client = httpx.AsyncClient()
        await self.client.__aenter__()
        return self

    async def __aexit__(
        self,
        exc_type: Type[BaseException],
        exc_value: BaseException,
        traceback: TracebackType,
    ) -> None:
        await self.client.__aexit__(exc_type, exc_value, traceback)
        del self._clients[-1]

    async def aclose(self) -> None:
        """Close the connection pool of the current inner client."""
        await self.client.aclose()

    async def request(
        self,
        path: str,
        method: str,
        query: Optional[Dict[Any, Any]] = None,
        body: Optional[Dict[Any, Any]] = None,
        form_data: Optional[Dict[Any, Any]] = None,
        auth: Optional[str] = None,
    ) -> Any:
        """Send an HTTP request asynchronously."""
        request = self._build_request(method, path, query, body, form_data, auth)
        try:
            response = await self.client.send(request)
        except httpx.TimeoutException:
            raise RequestTimeoutError()
        return self._parse_response(response)

aclose() async

Close the connection pool of the current inner client.

Source code in notion_client/client.py
259
260
261
async def aclose(self) -> None:
    """Close the connection pool of the current inner client."""
    await self.client.aclose()

request(path, method, query=None, body=None, form_data=None, auth=None) async

Send an HTTP request asynchronously.

Source code in notion_client/client.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
async def request(
    self,
    path: str,
    method: str,
    query: Optional[Dict[Any, Any]] = None,
    body: Optional[Dict[Any, Any]] = None,
    form_data: Optional[Dict[Any, Any]] = None,
    auth: Optional[str] = None,
) -> Any:
    """Send an HTTP request asynchronously."""
    request = self._build_request(method, path, query, body, form_data, auth)
    try:
        response = await self.client.send(request)
    except httpx.TimeoutException:
        raise RequestTimeoutError()
    return self._parse_response(response)

Client

Bases: BaseClient

Synchronous client for Notion's API.

Source code in notion_client/client.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class Client(BaseClient):
    """Synchronous client for Notion's API."""

    client: httpx.Client

    def __init__(
        self,
        options: Optional[Union[Dict[Any, Any], ClientOptions]] = None,
        client: Optional[httpx.Client] = None,
        **kwargs: Any,
    ) -> None:
        if client is None:
            client = httpx.Client()
        super().__init__(client, options, **kwargs)

    def __enter__(self) -> "Client":
        self.client = httpx.Client()
        self.client.__enter__()
        return self

    def __exit__(
        self,
        exc_type: Type[BaseException],
        exc_value: BaseException,
        traceback: TracebackType,
    ) -> None:
        self.client.__exit__(exc_type, exc_value, traceback)
        del self._clients[-1]

    def close(self) -> None:
        """Close the connection pool of the current inner client."""
        self.client.close()

    def request(
        self,
        path: str,
        method: str,
        query: Optional[Dict[Any, Any]] = None,
        body: Optional[Dict[Any, Any]] = None,
        form_data: Optional[Dict[Any, Any]] = None,
        auth: Optional[str] = None,
    ) -> Any:
        """Send an HTTP request."""
        request = self._build_request(method, path, query, body, form_data, auth)
        try:
            response = self.client.send(request)
        except httpx.TimeoutException:
            raise RequestTimeoutError()
        return self._parse_response(response)

close()

Close the connection pool of the current inner client.

Source code in notion_client/client.py
208
209
210
def close(self) -> None:
    """Close the connection pool of the current inner client."""
    self.client.close()

request(path, method, query=None, body=None, form_data=None, auth=None)

Send an HTTP request.

Source code in notion_client/client.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def request(
    self,
    path: str,
    method: str,
    query: Optional[Dict[Any, Any]] = None,
    body: Optional[Dict[Any, Any]] = None,
    form_data: Optional[Dict[Any, Any]] = None,
    auth: Optional[str] = None,
) -> Any:
    """Send an HTTP request."""
    request = self._build_request(method, path, query, body, form_data, auth)
    try:
        response = self.client.send(request)
    except httpx.TimeoutException:
        raise RequestTimeoutError()
    return self._parse_response(response)

ClientOptions dataclass

Options to configure the client.

Attributes:

Name Type Description
auth Optional[str]

Bearer token for authentication. If left undefined, the auth parameter should be set on each request.

timeout_ms int

Number of milliseconds to wait before emitting a RequestTimeoutError.

base_url str

The root URL for sending API requests. This can be changed to test with a mock server.

log_level int

Verbosity of logs the instance will produce. By default, logs are written to stdout.

logger Optional[Logger]

A custom logger.

notion_version str

Notion version to use.

Source code in notion_client/client.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@dataclass
class ClientOptions:
    """Options to configure the client.

    Attributes:
        auth: Bearer token for authentication. If left undefined, the `auth` parameter
            should be set on each request.
        timeout_ms: Number of milliseconds to wait before emitting a
            `RequestTimeoutError`.
        base_url: The root URL for sending API requests. This can be changed to test with
            a mock server.
        log_level: Verbosity of logs the instance will produce. By default, logs are
            written to `stdout`.
        logger: A custom logger.
        notion_version: Notion version to use.
    """

    auth: Optional[str] = None
    timeout_ms: int = 60_000
    base_url: str = "https://api.notion.com"
    log_level: int = logging.WARNING
    logger: Optional[logging.Logger] = None
    notion_version: str = "2022-06-28"