{"library":"mastodon-py","title":"Python wrapper for the Mastodon API","description":"Mastodon.py is an actively maintained Python wrapper for the Mastodon API, providing a feature-complete client for interacting with Mastodon instances. It is currently at version 2.2.1 and receives regular updates to support new Mastodon API features and fix bugs.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install Mastodon.py"],"cli":null},"imports":["from mastodon import Mastodon"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom mastodon import Mastodon\n\n# --- App Registration (Run once per server/app) ---\n# client_id, client_secret = Mastodon.create_app(\n#     'MyPythonApp',\n#     api_base_url = 'https://mastodon.social',\n#     to_file = 'pytooter_clientcred.secret'\n# )\n\n# --- User Login (Run once per user/app) ---\n# mastodon = Mastodon(client_id = 'pytooter_clientcred.secret')\n# # print(mastodon.auth_request_url())\n# # code = input(\"Enter the OAuth authorization code: \")\n# # mastodon.log_in(code=code, to_file=\"pytooter_usercred.secret\")\n\n# --- Usage with existing credentials ---\n# It's recommended to store sensitive tokens securely (e.g., environment variables).\n# For quick testing, you can load from file if previously saved.\n# ACCESS_TOKEN_FILE = 'pytooter_usercred.secret'\n# if os.path.exists(ACCESS_TOKEN_FILE):\n#     api_base_url = None # Will be loaded from file if present\n#     with open(ACCESS_TOKEN_FILE, 'r') as f:\n#         # Simple parsing for demo, real-world might use better config handling\n#         lines = f.readlines()\n#         for line in lines:\n#             if 'api_base_url' in line:\n#                 api_base_url = line.split('=')[1].strip()\n#                 break\n#     mastodon = Mastodon(access_token=ACCESS_TOKEN_FILE, api_base_url=api_base_url)\n# else:\nMASTODON_ACCESS_TOKEN = os.environ.get('MASTODON_ACCESS_TOKEN', '')\nMASTODON_API_BASE_URL = os.environ.get('MASTODON_API_BASE_URL', 'https://mastodon.social')\n\nif not MASTODON_ACCESS_TOKEN:\n    print(\"Error: MASTODON_ACCESS_TOKEN environment variable not set. Please set it or uncomment app registration/login code.\")\n    exit(1)\n\nprint(f\"Connecting to Mastodon instance: {MASTODON_API_BASE_URL}\")\nmastodon = Mastodon(\n    access_token=MASTODON_ACCESS_TOKEN,\n    api_base_url=MASTODON_API_BASE_URL\n)\n\ntry:\n    # Get current authenticated user's account details\n    me = mastodon.account_verify_credentials()\n    print(f\"Successfully authenticated as @{me.acct}\")\n\n    # Post a status (toot)\n    status = mastodon.status_post('Hello from mastodon-py! #python #mastodonpy')\n    print(f\"Tooted: {status.url}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to authenticate with a Mastodon instance using an access token and post a status (toot). It assumes you have already registered an application and obtained a user access token. For production, store your `MASTODON_ACCESS_TOKEN` and `MASTODON_API_BASE_URL` as environment variables. The commented-out sections show the `create_app` and `log_in` steps for initial setup.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}