Something went wrong. Try again.
Bluesky (AT Protocol) Client Library for Google Apps Script
Something went wrong. Try again.
Files
main Default
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 (
429and5xx). - 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 viasetCustomPDS().
- Content Creation & Management:
- Create text posts (
createRecord) with optional trailing URLs and rich inline link facets (story.linksby text match or UTF-16 character range, or rawstory.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).
- Create text posts (
- Social Interactions:
- Repost records (
repostRecord). - Retrieve actor profiles (
getProfile) and likes (getActorLikes). - Search for posts by handle, date range, limit, and sort order (
searchPosts).
- Repost records (
- Security: Support for
PropertiesServiceto 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().
- Unit Tests: Mocked tests that verify logic without making real network calls. Run via
appsscript.json&.clasp.json: Google Apps Script manifest andclaspconfiguration for local development and deployment.
Installation #
- Copy the code from
Bluesky.gsinto your Google Apps Script project (or push it usingclasp push). - (Optional) Copy
Tests.gsto verify your setup. - 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()inTests.gs. - To run real API tests: Execute
runIntegrationTests()inTests.gs(ensure credentials are set).