@cyberdrk/gram
v0.1.0
Published
CLI tool for interacting with Instagram via GraphQL/REST API
Readme
Gram
A CLI tool for interacting with Instagram via their undocumented GraphQL/REST API using cookie-based authentication.
Disclaimer
This project uses Instagram's undocumented web API (and cookie auth). Instagram can change endpoints, query IDs, and anti-bot behavior at any time — expect this to break without notice.
Install
npm install -g @arein/gram
# or
pnpm add -g @arein/gram
# or
bun add -g @arein/gram
# one-shot (no install)
bunx @arein/gram whoamiQuickstart
# Show the logged-in account
gram whoami
# View your home feed
gram feed
gram feed --count 50
# View the explore/discover feed
gram explore
# View a post
gram post CxyzABC1234
gram https://www.instagram.com/p/CxyzABC1234/
# View a user profile
gram user instagram
gram user @instagram --json
# Search for users, hashtags, places
gram search "coffee shop"
gram search "photography" --type hashtags
# Like/unlike posts
gram like CxyzABC1234
gram unlike CxyzABC1234Commands
Feed Commands
gram feed [-n count] [--json] [--json-full]— get your home feedgram explore [-n count] [--json] [--json-full]— get the explore/discover feed
Post Commands
gram post <shortcode-or-url> [--json] [--json-full]— view a post by shortcode or URLgram comments <shortcode> [-n count] [--json]— view comments on a postgram likers <shortcode> [--json]— view users who liked a postgram <shortcode-or-url>— shorthand forgram post <shortcode-or-url>
User Commands
gram user <username> [--json]— view a user profilegram posts <username> [-n count] [--json]— get a user's postsgram following [username] [-n count] [--json]— get users that someone follows (defaults to you)gram followers [username] [-n count] [--json]— get someone's followers (defaults to you)
Search
gram search <query> [--type <type>] [--json]— search for users, hashtags, and places- Types:
blended(default),users,hashtags,places
- Types:
Engagement
gram like <shortcode>— like a postgram unlike <shortcode>— unlike a postgram save <shortcode>— save/bookmark a postgram unsave <shortcode>— unsave/unbookmark a postgram comment <shortcode> "<text>"— comment on a postgram follow <username>— follow a usergram unfollow <username>— unfollow a user
Account
gram whoami [--json]— show the currently authenticated usergram check— check credential availability and sources
Query IDs
gram query-ids [--refresh] [--json]— inspect or refresh cached GraphQL query IDs
Authentication
Gram uses your existing Instagram web session. It resolves credentials in this order:
- CLI flags:
--session-id,--csrf-token,--ds-user-id - Environment variables:
SESSION_ID,CSRF_TOKEN,DS_USER_ID(or prefixed withINSTAGRAM_/IG_) - Browser cookies via
@steipete/sweet-cookie(Safari, Chrome, Firefox)
Required Cookies
sessionid— Your Instagram session cookiecsrftoken— Cross-site request forgery tokends_user_id— Your user ID (optional but recommended)
Browser Cookie Sources
- Safari:
~/Library/Cookies/Cookies.binarycookies - Chrome:
~/Library/Application Support/Google/Chrome/<Profile>/Cookies - Firefox:
~/Library/Application Support/Firefox/Profiles/<profile>/cookies.sqlite
Override browser order via --cookie-source (repeatable).
Config (JSON5)
Config precedence: CLI flags > env vars > project config > global config.
- Global:
~/.config/gram/config.json5 - Project:
./.gramrc.json5
Example ~/.config/gram/config.json5:
{
// Cookie source order for browser extraction
cookieSource: ["safari", "chrome", "firefox"],
chromeProfile: "Profile 1",
chromeProfileDir: "/path/to/Chrome/Profile",
firefoxProfile: "default-release",
cookieTimeoutMs: 30000,
timeoutMs: 60000
}Environment shortcuts:
GRAM_TIMEOUT_MSGRAM_COOKIE_TIMEOUT_MS
Global Options
--session-id <token>: Instagram sessionid cookie--csrf-token <token>: Instagram csrftoken cookie--ds-user-id <id>: Instagram ds_user_id cookie--cookie-source <safari|chrome|firefox>: browser cookie source (repeatable)--chrome-profile <name>: Chrome profile name--chrome-profile-dir <path>: Chrome/Chromium profile directory--firefox-profile <name>: Firefox profile name--cookie-timeout <ms>: cookie extraction timeout (milliseconds)--timeout <ms>: request timeout (milliseconds)--plain: stable output (no emoji, no color)--no-emoji: disable emoji output--no-color: disable ANSI colors (or setNO_COLOR=1)
Output Formats
- Default: Human-readable terminal output with emoji
--json: Machine-readable JSON--json-full: JSON with raw API response in_rawfield--plain: Stable output without emoji or colors
Library Usage
Gram can be used as a library:
import { InstagramClient, resolveCredentials } from '@arein/gram';
const { cookies } = await resolveCredentials({ cookieSource: 'safari' });
const client = new InstagramClient({ cookies });
// Get user profile
const userResult = await client.getUser('instagram');
if (userResult.success) {
console.log(userResult.user);
}
// Get home feed
const feedResult = await client.getHomeFeed();
if (feedResult.success) {
console.log(feedResult.posts);
}
// Like a post
const post = await client.getPost('CxyzABC1234');
if (post.success) {
await client.like(post.post.id);
}
// Search
const searchResult = await client.search('coffee', 'users');Query IDs
Instagram's web app uses query IDs that rotate periodically. Gram:
- Ships with baseline IDs in
src/lib/query-ids.json - Caches runtime-discovered IDs at
~/.config/gram/query-ids-cache.json(24h TTL) - Can refresh on demand via
gram query-ids --refresh
Override cache path: GRAM_QUERY_IDS_CACHE=/path/to/file.json
Development
pnpm install
pnpm run build # dist/ + bun binary
pnpm run build:dist # dist/ only
pnpm test
pnpm run lintLicense
MIT
