Bluesky (AT Protocol) Client Library for Google Apps Script
README.md

Bluesky/AT Protocol Library for Google Apps Script #

A lightweight and robust library to interact with the Bluesky (AT Protocol) API directly from Google Apps Script.

Features #

  • Centralized API Handling: All network calls are routed through a secure, internal fetcher that handles headers, authentication, error management, and automatic retries on transient errors (429 and 5xx).
  • Session & PDS Management:
    • Easy creation and retrieval of AT Protocol sessions with built-in session validation guards.
    • Automatic PDS detection from handle suffixes (e.g., .bsky.social, .eurosky.social) or manual configuration via setCustomPDS().
  • Content Creation & Management:
    • Create text posts (createRecord) with optional trailing URLs and rich inline link facets (story.links by text match or UTF-16 character range, or raw story.facets).
    • Create posts with embedded records/quotes (createRecordWithEmbeddedPost).
    • Create posts with embedded images (createRecordWithEmbeddedImage), preserving the original image MIME type and automatically extracting aspect ratio dimensions from both JPEG (baseline & progressive) and PNG headers.
    • Create posts with external link cards (createRecordWithExternalLink / app.bsky.embed.external), optional thumbnail blobs, and inline link facets.
    • Delete records (deleteRecord).
  • Social Interactions:
    • Repost records (repostRecord).
    • Retrieve actor profiles (getProfile) and likes (getActorLikes).
    • Search for posts by handle, date range, limit, and sort order (searchPosts).
  • Security: Support for PropertiesService to keep credentials out of the source code.

Project Structure #

  • Bluesky.gs: The main library containing all AT Protocol methods.
  • Tests.gs: A comprehensive testing suite.
    • Unit Tests: Mocked tests that verify logic without making real network calls. Run via runAllTests().
    • Integration Tests: Real-world tests to verify end-to-end functionality. Run via runIntegrationTests().
  • appsscript.json & .clasp.json: Google Apps Script manifest and clasp configuration for local development and deployment.

Installation #

  1. Copy the code from Bluesky.gs into your Google Apps Script project (or push it using clasp push).
  2. (Optional) Copy Tests.gs to verify your setup.
  3. Configure your credentials in Project Settings > Script Properties:
    • BSKY_USER: Your AT Protocol handle or DID.
    • BSKY_PWD: Your app-specific password.

Quick Start #

Simple Post with Trailing Link #

function post() {
  const user = PropertiesService.getScriptProperties().getProperty('BSKY_USER');
  const pwd = PropertiesService.getScriptProperties().getProperty('BSKY_PWD');
  
  const session = createSession(user, pwd);
  const story = { 
    title: "Hello from Google Apps Script!", 
    url: "https://example.com" 
  };
  
  createRecord(session, story, "en");
}

Post with Multiple Inline Links #

You can attach multiple inline link facets using story.links (matching by text substring or explicit UTF-16 start/end indices) or pass raw AT Protocol facets via story.facets:

function postWithInlineLinks() {
  const user = PropertiesService.getScriptProperties().getProperty('BSKY_USER');
  const pwd = PropertiesService.getScriptProperties().getProperty('BSKY_PWD');

  const session = createSession(user, pwd);
  const story = {
    title: "Read the docs and check the source code!",
    links: [
      { text: "docs", url: "https://atproto.com" },
      { text: "source code", url: "https://example.com/repo" }
    ]
  };

  createRecord(session, story, "en");
}

Post with External Link Card #

function postExternalCard() {
  const user = PropertiesService.getScriptProperties().getProperty('BSKY_USER');
  const pwd = PropertiesService.getScriptProperties().getProperty('BSKY_PWD');

  const session = createSession(user, pwd);
  const story = {
    text: "Check out this article!",
    title: "Example Article",
    description: "An overview of the AT Protocol library for Google Apps Script.",
    url: "https://example.com/article",
    links: [
      { text: "this article", url: "https://example.com/article" }
    ]
  };

  createRecordWithExternalLink(session, story, "https://example.com/thumb.jpg", "en");
}

Testing #

The library includes a custom mocking framework for UrlFetchApp.

  • To run logic-only tests: Execute runAllTests() in Tests.gs.
  • To run real API tests: Execute runIntegrationTests() in Tests.gs (ensure credentials are set).