Lark (Feishu) OpenAPI Python SDK

1.5.3 · active · verified Sat Apr 11

The `lark-oapi` library is the official Python SDK for interacting with the Lark (Feishu) Open Platform APIs. It provides a programmatic interface for accessing Lark's various services, including messaging, contacts, calendar, and more. Currently at version 1.5.3, it follows a regular release cadence with updates reflecting API changes and new features from the Lark Open Platform.

Warnings

Install

Imports

Quickstart

This quickstart initializes the Lark OpenAPI client using `LARK_APP_ID` and `LARK_APP_SECRET` from environment variables. It then attempts to list a single message from a placeholder chat ID using the `im.v1.message.list` API. For successful execution, you must replace `oc_xxxxxxxx` with a valid chat ID and ensure your Lark app has the necessary permissions (e.g., 'Read messages from group chats it joined').

import os
from lark_oapi import Client, AppSettings
from lark_oapi.api.im.v1 import ListMessageRequest

# Get credentials from environment variables
# Register your app on the Lark/Feishu Open Platform to get APP_ID and APP_SECRET.
app_settings = AppSettings(
    app_id=os.environ.get("LARK_APP_ID", ""),
    app_secret=os.environ.get("LARK_APP_SECRET", ""),
    # For event handling, also set:
    # verification_token=os.environ.get("LARK_VERIFICATION_TOKEN", ""),
    # encrypt_key=os.environ.get("LARK_ENCRYPT_KEY", ""),
)

# Build the client
client = Client.builder() \
    .app_settings(app_settings) \
    .log_level(Client.LOG_LEVEL_INFO) \
    .build()

# Example: List messages in a specific chat
# NOTE: Replace "oc_xxxxxxxx" with an actual chat ID your app has access to.
#       Ensure your Lark app has 'Read messages from group chats it joined' permission.
print("Attempting to list messages...")
try:
    # This request attempts to fetch the latest message from a specified chat.
    # For a real scenario, you'd need a valid chat_id (e.g., from an event subscription).
    request = ListMessageRequest.builder().chat_id("oc_xxxxxxxx").page_size(1).build()
    response = client.im.v1.message.list(request)

    if response.code == 0:
        print("Successfully listed messages:")
        if response.data and response.data.items:
            for message in response.data.items:
                print(f"  Message ID: {message.message_id}, Content: {message.body.content if message.body else 'N/A'}")
        else:
            print("  No messages found or data empty.")
    else:
        print(f"Failed to list messages: {response.code} - {response.msg}")
        # Detailed error info often in response.error
        if response.error:
            print(f"  Error details: {response.error}")

except Exception as e:
    print(f"An unexpected error occurred: {e}")

print("Quickstart finished.")

view raw JSON →