Simple Python SDK for AmoCRM API
Project description
amocrm-sdk
Python SDK for the AmoCRM REST API.
Features
- OAuth2 с auto-refresh токенов по 401
- Гибкое хранилище токенов — любой класс с
save()/load()(Django ORM, Redis, etc.) DjangoTokenStorageиз коробки- Глобальный менеджер (
exchange_code/get_client) для Django/Flask - Typed DTO models — никаких сырых словарей
- Leads (сделки): list, get, create, update, update_one, create_complex;
get()подгружает контакты по умолчанию - Contacts (контакты): list, get, create, update, update_one
- Companies (компании): list, get, create, update, update_one
- Pipelines (воронки): list, get, create, update, delete + statuses CRUD
- Tasks (задачи): list, get, create, update, update_one
- Лимит 50 элементов за batch-запрос (create/update) — для всех ресурсов
- Custom fields support
- Авто-пагинация —
list()безpageавтоматически обходит все страницы и возвращаетIterator[T] - Кастомные DTO —
configure_dto()позволяет подменить базовые классы своими подклассами
Installation
uv add amocrm-sdk
# or
pip install amocrm-sdk
Quick Start
OAuth2 setup
from amocrm import OAuthConfig
from amocrm.auth import DjangoTokenStorage
from amocrm.manager import exchange_code, get_client
oauth = OAuthConfig(
client_id="...",
client_secret="...",
redirect_uri="https://yourapp.com/oauth/callback",
storage=DjangoTokenStorage(AmoCRMToken.objects.get(id=1)),
)
# Первый запуск — обменять код авторизации на токены
exchange_code(subdomain="your-company", code=auth_code, oauth=oauth)
# Последующие запуски — токены загружаются из storage автоматически
client = get_client(subdomain="your-company", oauth=oauth)
Кастомное хранилище токенов
# Пример с Redis
class RedisTokenStorage:
def save(self, access_token: str, refresh_token: str) -> None:
redis.set("amo:access", access_token)
redis.set("amo:refresh", refresh_token)
def load(self) -> tuple[str, str]:
return redis.get("amo:access"), redis.get("amo:refresh")
Типичный порядок работы
from amocrm import OAuthConfig, Lead, Contact, Company
from amocrm.auth import DjangoTokenStorage
from amocrm.manager import exchange_code, get_client
oauth = OAuthConfig(
client_id="...",
client_secret="...",
redirect_uri="https://yourapp.com/oauth/callback",
storage=DjangoTokenStorage(AmoCRMToken.objects.get(id=1)),
)
# 1. Обмен кода авторизации на токены (OAuth callback)
exchange_code(subdomain="your-company", code=auth_code, oauth=oauth)
client = get_client(subdomain="your-company", oauth=oauth)
# 2. Создание сделки с контактом и компанией (create_complex)
lead = Lead(
name="Новая сделка",
price=100000,
contacts=[Contact(name="Иван Иванов")],
company=Company(name="ООО Ромашка"),
)
created = client.leads.create_complex([lead])
lead_id = created[0].id
# 3. Получение лида с контактом и компанией (get подгружает contacts по умолчанию)
lead = client.leads.get(lead_id)
contact = lead.contacts[0] # Contact
company = lead.company # Company
# 4. Обновление контакта
contact.name = "Иван Петров"
client.contacts.update_one(contact)
# 5. Обновление компании
company.name = "ООО Лютик"
client.companies.update_one(company)
Пагинация
Методы list() у всех ресурсов поддерживают два режима:
| Вызов | Поведение | Возвращает |
|---|---|---|
client.leads.list() |
Авто-пагинация — обходит все страницы | Iterator[Lead] |
client.leads.list(page=2) |
Одна конкретная страница | list[Lead] |
По умолчанию авто-пагинация запрашивает по 50 элементов на страницу. Для кастомного размера: client.leads.list(limit=250).
Кодогенератор (Codegen)
Генерирует типизированные подклассы DTO с named-свойствами для кастомных полей AmoCRM. Имена свойств автоматически транслитерируются из кириллицы в латиницу.
from amocrm import OAuthConfig, DjangoTokenStorage
from amocrm.codegen import generate_custom_fields_dto
oauth = OAuthConfig(client_id="...", client_secret="...", redirect_uri="...",
storage=DjangoTokenStorage(django_obj))
generate_custom_fields_dto("mycompany", oauth, output_file="src/amocrm/models/my_models.py")
Подключение кастомных DTO
После кодогенерации подключите свои классы к клиенту через configure_dto() — все методы
get, list, create, update начнут возвращать экземпляры вашего подкласса:
from amocrm.models.my_models import MyLead, MyContact, MyCompany
client = get_client(subdomain="mycompany", oauth=oauth)
client.configure_dto(leads=MyLead, contacts=MyContact, companies=MyCompany)
lead = client.leads.get(123) # → MyLead
lead.inn # → str | None (геттер из MyLead)
lead.inn = "new_inn" # записывает в custom_fields_values
client.leads.update([lead]) # сохраняет изменения в AmoCRM
for lead in client.leads.list():
print(lead.istochnik_trafika) # → str | None
Подробнее — в документации.
Links
TODO
- https://www.amocrm.ru/developers/content/api/recommendations
- https://www.amocrm.ru/developers/content/crm_platform/account-info
https://www.amocrm.ru/developers/content/crm_platform/leads-api- https://www.amocrm.ru/developers/content/crm_platform/unsorted-api
https://www.amocrm.ru/developers/content/crm_platform/leads_pipelines- https://www.amocrm.ru/developers/content/crm_platform/filters-api
https://www.amocrm.ru/developers/content/crm_platform/contacts-apihttps://www.amocrm.ru/developers/content/crm_platform/companies-api- https://www.amocrm.ru/developers/content/crm_platform/catalogs-api
- https://www.amocrm.ru/developers/content/crm_platform/entity-links-api
https://www.amocrm.ru/developers/content/crm_platform/tasks-api- https://www.amocrm.ru/developers/content/crm_platform/custom-fields
- https://www.amocrm.ru/developers/content/crm_platform/tags-api
- https://www.amocrm.ru/developers/content/crm_platform/events-and-notes
- https://www.amocrm.ru/developers/content/crm_platform/customers-api
- https://www.amocrm.ru/developers/content/crm_platform/customers-statuses-api
- https://www.amocrm.ru/developers/content/crm_platform/users-api
- https://www.amocrm.ru/developers/content/crm_platform/products-api
- https://www.amocrm.ru/developers/content/crm_platform/webhooks-api
- https://www.amocrm.ru/developers/content/crm_platform/widgets-api
- https://www.amocrm.ru/developers/content/crm_platform/calls-api
- https://www.amocrm.ru/developers/content/crm_platform/talks-api
- https://www.amocrm.ru/developers/content/crm_platform/subscriptions-api
- https://www.amocrm.ru/developers/content/crm_platform/sources-api
- https://www.amocrm.ru/developers/content/api/salesbot-api
- https://www.amocrm.ru/developers/content/crm_platform/short_links
- https://www.amocrm.ru/developers/content/crm_platform/chat-templates-api
- https://www.amocrm.ru/developers/content/crm_platform/duplication-control
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file amocrm_sdk-0.4.0.tar.gz.
File metadata
- Download URL: amocrm_sdk-0.4.0.tar.gz
- Upload date:
- Size: 8.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d1c08e9fda89641e47824b03373447c6089c67ad85674e8b0475895478beca7
|
|
| MD5 |
0111d864c3516f23c4deda46732cb739
|
|
| BLAKE2b-256 |
3d5a735f4ef556c146063f9ebc0abfa6daad3db8429c4a809991c6c8d7396965
|
Provenance
The following attestation bundles were made for amocrm_sdk-0.4.0.tar.gz:
Publisher:
publish.yml on MikhailMurashov/amocrm-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
amocrm_sdk-0.4.0.tar.gz -
Subject digest:
8d1c08e9fda89641e47824b03373447c6089c67ad85674e8b0475895478beca7 - Sigstore transparency entry: 1116003787
- Sigstore integration time:
-
Permalink:
MikhailMurashov/amocrm-sdk@b200e189a12e5e1be5f4690cd731f7e91abcf835 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/MikhailMurashov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b200e189a12e5e1be5f4690cd731f7e91abcf835 -
Trigger Event:
release
-
Statement type:
File details
Details for the file amocrm_sdk-0.4.0-py3-none-any.whl.
File metadata
- Download URL: amocrm_sdk-0.4.0-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adfdd8801b3b74b116732b31cb569ece40a4241c111d07602489dec56b659959
|
|
| MD5 |
bf015355de6dcb70588359db4c163aa0
|
|
| BLAKE2b-256 |
396b6e2504cd1001e5fed161af9f6a08b7aaa5d822a9a96c7f2124b6f42b9462
|
Provenance
The following attestation bundles were made for amocrm_sdk-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on MikhailMurashov/amocrm-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
amocrm_sdk-0.4.0-py3-none-any.whl -
Subject digest:
adfdd8801b3b74b116732b31cb569ece40a4241c111d07602489dec56b659959 - Sigstore transparency entry: 1116004290
- Sigstore integration time:
-
Permalink:
MikhailMurashov/amocrm-sdk@b200e189a12e5e1be5f4690cd731f7e91abcf835 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/MikhailMurashov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b200e189a12e5e1be5f4690cd731f7e91abcf835 -
Trigger Event:
release
-
Statement type: