# coze-py
**Repository Path**: ai-large-model-tool/coze-py
## Basic Information
- **Project Name**: coze-py
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-08-03
- **Last Updated**: 2025-08-03
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Coze Python API SDK
[](https://pypi.org/project/cozepy/)

[](https://codecov.io/github/coze-dev/coze-py)
## Introduction
The Coze Python SDK is your comprehensive solution for seamlessly integrating Coze's powerful open APIs into Python applications.
- Complete API coverage: All Coze open APIs and authentication methods supported
- Dual interface: Both synchronous and asynchronous SDK calls available
- Stream-optimized: Native Stream and AsyncStream objects for real-time data
- Pagination made easy: Iterator-based Page objects for efficient list operations
- Developer-first: Intuitive API design for rapid integration
## Requirements
Python 3.7 or higher.
## Install
```shell
pip install cozepy
```
## Usage
### Examples
### Initialize client
Start by obtaining your access token from the Coze platform:
- For coze.cn: Visit [https://www.coze.cn/open/oauth/pats](https://www.coze.cn/open/oauth/pats)
- For coze.com: Visit [https://www.coze.com/open/oauth/pats](https://www.coze.com/open/oauth/pats)
Create a new personal access token by specifying a name, expiration period, and required permissions. Store this token securely—never expose it in code or version control.
```python
import os
from cozepy import Coze, TokenAuth, COZE_CN_BASE_URL, AsyncCoze, AsyncTokenAuth
# Get an access_token through personal access token or oauth.
coze_api_token = os.getenv("COZE_API_TOKEN")
# The default access is api.coze.com, but if you need to access api.coze.cn,
# please use base_url to configure the api endpoint to access
coze_api_base = os.getenv("COZE_API_BASE") or COZE_CN_BASE_URL
# init coze with token and base_url
coze = Coze(auth=TokenAuth(coze_api_token), base_url=coze_api_base)
async_coze = AsyncCoze(auth=AsyncTokenAuth(coze_api_token), base_url=coze_api_base)
```
coze api access_token can also be generated via the OAuth App. For details, refer to:
- [Web OAuth](./examples/auth_oauth_web.py)
- [JWT OAuth](./examples/auth_oauth_jwt.py)
- [PKCE OAuth](./examples/auth_oauth_pkce.py)
- [Device OAuth](./examples/auth_oauth_device.py)
### Bot Chat
Create your bot in Coze and extract the bot ID from the URL (the final numeric segment).
Use `coze.chat.stream` for real-time streaming conversations. This method returns a Chat Iterator that yields events as they occur—simply iterate through the stream to process each event.
```python
import os
from cozepy import Coze, TokenAuth, Message, ChatEventType, COZE_CN_BASE_URL
# initialize client
coze_api_token = os.getenv("COZE_API_TOKEN")
coze_api_base = os.getenv("COZE_API_BASE") or COZE_CN_BASE_URL
coze = Coze(auth=TokenAuth(coze_api_token), base_url=coze_api_base)
# The return values of the streaming interface can be iterated immediately.
for event in coze.chat.stream(
# id of bot
bot_id='bot_id',
# id of user, Note: The user_id here is specified by the developer, for example, it can be the
# business id in the developer system, and does not include the internal attributes of coze.
user_id='user_id',
# user input
additional_messages=[Message.build_user_question_text("How are you?")]
# conversation id, for Chaining conversation context
# conversation_id='