A comprehensive collection of Node.js utilities for Unsplash image management, including batch downloading, caching, and API integration with support for premium Unsplash+ content.
- 🖼️ Batch Image Downloading - Download multiple images from Unsplash with concurrent processing
- 🎯 Smart Caching System - Build-time and runtime caching with Redis support
- 🔐 Premium Unsplash+ Support - Watermark-free image access for subscribers
- 📋 Manifest Generation - Create static manifests for build-time optimization
- 🧹 Cleanup Tools - Remove unused cached images and clean storage
- ✅ API Compliance - Automatic download tracking and attribution handling
- 🔍 URL Utilities - Convert Unsplash page URLs to direct download links
- 📊 Progress Tracking - Real-time progress bars and detailed logging
# Install as a dependency
npm install @nicholasadamou/unsplash-node-utilities
# Or install globally for CLI usage
npm install -g @nicholasadamou/unsplash-node-utilities
Create a .env
file with your Unsplash API credentials:
UNSPLASH_ACCESS_KEY=your-access-key-here
UNSPLASH_SECRET_KEY=your-secret-key-here # Required for Unsplash+ premium access
# Optional: Redis Cache Configuration
REDIS_URL=your-redis-url
# OR
UPSTASH_REDIS_REST_URL=your-upstash-url
# Scan content directory and build manifest
npm run cache:build
# Or use CLI
unsplash-build-cache
# Download all images from manifest
npm run download
# Or use CLI
unsplash-download
The package provides several CLI utilities:
# Build cache manifest from content files
unsplash-build-cache
# Runtime image caching
unsplash-cache
# Download images from manifest
unsplash-download
# Clean unused images
unsplash-clean
# Download individual images with size options
unsplash-image-downloader "https://unsplash.com/photos/example-abc123"
# Download specific size to custom directory
unsplash-image-downloader "https://unsplash.com/photos/example-abc123" --size=full --output=./images
# Download with custom dimensions and format
unsplash-image-downloader "https://unsplash.com/photos/example-abc123" --size=custom --width=1920 --height=1080 --format=webp --quality=90
# Download unwatermarked Unsplash+ image (with ixid)
unsplash-image-downloader "https://unsplash.com/photos/example-abc123/download?ixid=M3wxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNzU2MjU5NjgzfA&force=true"
# Verify Unsplash account and API access
unsplash-verify
# Convert Unsplash page URLs to download URLs
unsplash-url-to-download "https://unsplash.com/photos/example-abc123"
const {
extractPhotoId,
fetchImageData,
createPremiumUnsplashUrl,
scanMdxFiles
} = require('@nicholasadamou/unsplash-node-utilities');
// Or directly from the library:
// const { ... } = require('@nicholasadamou/unsplash-node-utilities/src/lib');
// Extract photo ID from various URL formats
const photoId = extractPhotoId('https://unsplash.com/photos/beautiful-sunset-abc123');
// Fetch image data with automatic premium optimization
const imageData = await fetchImageData(photoId);
// Scan content files for Unsplash URLs
const urls = await scanMdxFiles('./content');
const {
generateDownloadUrl,
convertToDownloadUrl,
createPremiumUnsplashUrl
} = require('@nicholasadamou/unsplash-node-utilities');
// Generate direct download URL
const downloadUrl = generateDownloadUrl('abc123', 'optional-ixid');
// Convert page URL to download URL with ixid fetching
const result = await convertToDownloadUrl(
'https://unsplash.com/photos/beautiful-sunset-abc123',
true // fetch ixid for better tracking
);
// Create premium watermark-free URL
const premiumUrl = createPremiumUnsplashUrl(
'https://plus.unsplash.com/premium-photo-123',
'abc123',
1200, // width
80 // quality
);
const {
scanMdxFiles,
sanitizeFilename,
getImageExtension
} = require('@nicholasadamou/unsplash-node-utilities');
// Scan MDX files for Unsplash URLs
const urls = await scanMdxFiles('./content');
// Sanitize filenames for safe storage
const safeFilename = sanitizeFilename('Photo by John Doe!.jpg');
// Get image extension from URL
const extension = getImageExtension('https://images.unsplash.com/photo-123?fm=webp');
The library includes configurable settings:
const { CONFIG } = require('@nicholasadamou/unsplash-node-utilities');
// Available configurations:
// {
// timeout: 10000, // 10 seconds timeout for HTTP requests
// userAgent: 'Mozilla/5.0 (compatible; Unsplash-Script-Library/1.0)',
// retries: 2, // Number of retry attempts
// rateLimitDelay: 100 // Milliseconds between API calls
// }
The utilities automatically handle premium content when:
- You have an Unsplash+ subscription
- Your API application is linked to your subscription
- The
UNSPLASH_SECRET_KEY
is configured - The image URL is from
plus.unsplash.com
const premiumUrl = createPremiumUnsplashUrl(
'https://plus.unsplash.com/premium-photo-123',
'abc123',
1200, // width
80 // quality
);
The library implements comprehensive error handling:
- Graceful Degradation - Functions return null or default values instead of throwing
- Timeout Protection - HTTP requests include configurable timeouts
- Rate Limit Awareness - Detects and handles API rate limit responses
- Retry Logic - Built-in retry mechanisms for failed operations
The utilities ensure automatic compliance with Unsplash API requirements:
- ✅ Download Tracking - Automatically triggers download endpoints
- ✅ Attribution Handling - Preserves photographer attribution data
- ✅ Rate Limit Respect - Implements delays and retry logic
- ✅ Terms Compliance - Follows Unsplash API terms of service
# Install in your Next.js project
npm install @nicholasadamou/unsplash-node-utilities
# Add to package.json scripts
{
"scripts": {
"prebuild": "npm-run-all unsplash:cache unsplash:download",
"unsplash:cache": "unsplash-build-cache",
"unsplash:download": "unsplash-download"
}
}
// scripts/build-images.js
const { scanMdxFiles, fetchImageData } = require('@nicholasadamou/unsplash-node-utilities');
async function buildImageCache() {
const urls = await scanMdxFiles('./content');
const manifest = {};
for (const url of urls) {
const photoId = extractPhotoId(url);
if (photoId) {
const data = await fetchImageData(photoId);
if (data) manifest[photoId] = data;
}
}
// Save manifest...
}
The project is organized as follows:
unsplash-node-utilities/
├── src/
│ ├── lib/ # Core library functionality
│ │ └── index.js # Main library exports
│ ├── cli/ # Command-line interface tools
│ │ ├── build-cache.js
│ │ ├── cache.js
│ │ ├── clean.js
│ │ ├── download.js
│ │ ├── image-downloader.js
│ │ ├── url-to-download.js
│ │ └── verify.js
│ ├── tools/ # Development and debugging tools
│ │ ├── browser-download.js
│ │ ├── get-download-url.js
│ │ ├── manual-download.js
│ │ └── simulate-browser.js
│ └── tests/ # Test files
│ ├── download-url.test.js
│ ├── fallback.test.js
│ └── integration.test.js
├── package.json
├── README.md
└── LICENSE
src/lib/
- Core library functionality exported as the main modulesrc/cli/
- Executable scripts available as npm bin commandssrc/tools/
- Development tools for debugging and testingsrc/tests/
- Test suites for validating functionality
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
Made with ❤️ for the developer community