Skip to main content

Lark OpenAPI SDK for Python

Project description

Feishu OpenPlatform Server SDK for Python

中文

The Feishu Open Platform provides server-side APIs for messaging, contacts, approval, sheets, Base, and many other product capabilities. This SDK wraps the repeated platform work around API calls, including token management, request signing, encryption/decryption, event dispatching, and typed request/response models.

Documentation

Installation

pip install lark-oapi

Python 3.8 or later is required.

Basic Usage

import lark_oapi as lark
from lark_oapi.api.im.v1 import *

client = lark.Client.builder() \
    .app_id("cli_xxx") \
    .app_secret("your_app_secret") \
    .build()

request = CreateMessageRequest.builder() \
    .receive_id_type("chat_id") \
    .request_body(CreateMessageRequestBody.builder()
        .receive_id("oc_xxx")
        .msg_type("text")
        .content("{\"text\":\"hello world\"}")
        .build()) \
    .build()

response = client.im.v1.message.create(request)

One-Click App Registration

lark_oapi.register_app creates an app through the OAuth device flow. It returns a verification URL in on_qr_code; render the URL as a QR code or show it as a link for the user to open in Feishu/Lark.

import lark_oapi as lark


def on_qr_code(info):
    print(info["url"])


result = lark.register_app(
    on_qr_code=on_qr_code,
    app_preset={
        "avatar": [
            "https://example.com/a.png",
            "https://example.com/b.webp",
        ],
        "name": "{user}'s app",
        "desc": "Created by the business platform",
    },
)

print(result["client_id"])

For a real manual E2E run without mocked registration responses:

python3 samples/registration/app_preset_live_e2e.py --open

register_app parameters

Parameter Description Type Required Default
on_qr_code Callback when the verification URL is ready. Receives {"url": str, "expire_in": int} function Yes -
on_status_change Callback on polling status changes. Status values include polling, slow_down, domain_switched function No -
source Source identifier appended to the QR URL as python-sdk/{source} string No python-sdk
cancel_event threading.Event used to cancel sync polling threading.Event No -
domain Custom Feishu accounts base URL string No https://accounts.feishu.cn
lark_domain Custom Lark accounts base URL used when tenant brand is Lark string No https://accounts.larksuite.com
app_preset Pre-fill values for the app-creation page. All fields are optional; users can still edit them on the page. Pass raw values; the SDK URL-encodes them automatically dict No -
app_preset.avatar App avatar URL(s). 1-6 URLs supported; the first one is selected by default. Allowed formats are handled by the Web page: png / jpg / jpeg / webp / gif string or list[string] No -
app_preset.name App name. Supports the {user} placeholder, replaced by the Web page with the scanning user's name string No -
app_preset.desc App description. Supports the {user} placeholder string No -

Channel Module

lark_oapi.channel is a high-level module built on top of the OpenAPI client and event transport. It bundles event listening, message normalization, safety policy, outbound sending, media upload/download, card interactions, and streaming replies into a single FeishuChannel entry point.

Use Channel when you are building a conversational bot that needs normalized message events, replies, media handling, card callbacks, mention policy, or WebSocket/webhook transport management.

import asyncio
import os

from lark_oapi.channel import FeishuChannel

channel = FeishuChannel(
    app_id=os.environ["LARK_APP_ID"],
    app_secret=os.environ["LARK_APP_SECRET"],
)

async def on_message(msg):
    await channel.send(
        msg.chat_id,
        {"text": f"echo: {msg.content_text}"},
    )

channel.on("message", on_message)

asyncio.run(channel.connect())

Full Channel documentation:

Examples

More composite API examples and business scenario samples are available in oapi-sdk-python-demo.

License

MIT

Contact Us

Click Server SDK in the upper right corner of the documentation page and submit feedback.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lark_oapi-1.6.7-py3-none-any.whl (7.1 MB view details)

Uploaded Python 3

File details

Details for the file lark_oapi-1.6.7-py3-none-any.whl.

File metadata

  • Download URL: lark_oapi-1.6.7-py3-none-any.whl
  • Upload date:
  • Size: 7.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for lark_oapi-1.6.7-py3-none-any.whl
Algorithm Hash digest
SHA256 df1d44891d266f5c063daa1d37ae6f72c7f166bdc2fb01e607088410e952b92c
MD5 594c360ba3d26cc4fcfb107cb1b4982f
BLAKE2b-256 2454a3b649b83299606aa7ebfd2391663fde650e934421dfba37af171bfbf456

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page