@svg.dog/svgdog
v1.0.0
Published
Official Node.js client for the svg.dog SVG optimization API
Maintainers
Readme
svgdog
The fastest way to optimize SVGs via API. Hosted SVG optimization powered by svg.dog — no SVGO config, no maintenance, always up to date.
npm install @svg.dog/svgdogimport { optimize } from '@svg.dog/svgdog';
import fs from 'node:fs';
const result = await optimize(fs.readFileSync('icon.svg'), { apiKey: 'YOUR_KEY' });
fs.writeFileSync('icon.min.svg', result.svg);
// → Saved 74.3%Get a free API key at svg.dog →
Why use this instead of running SVGO locally?
| | svgdog | SVGO locally |
|---|---|---|
| Setup | npm install @svg.dog/svgdog | Install + configure |
| Always latest SVGO | Yes | Manual updates |
| Works in any language | Yes (REST API) | Node.js only |
| Usage tracking | Yes | No |
| Budget cap | Yes | No |
| CI/CD friendly | Yes | Yes |
Installation
npm install @svg.dog/svgdog
# or
yarn add @svg.dog/svgdog
# or
pnpm add @svg.dog/svgdogRequires Node.js 18+ (uses native fetch). Zero runtime dependencies.
Quick start
1. Get an API key
Sign up for free at svg.dog — 500 optimizations per month included, no credit card required.
2. Optimize a file
import { optimize } from '@svg.dog/svgdog';
import fs from 'node:fs';
const result = await optimize(fs.readFileSync('logo.svg'), {
apiKey: process.env.SVGDOG_API_KEY!,
});
console.log(`Original: ${result.originalSize} bytes`);
console.log(`Optimized: ${result.optimizedSize} bytes`);
console.log(`Saved: ${result.savingsPercent.toFixed(1)}%`);
fs.writeFileSync('logo.min.svg', result.svg);3. Use in a CI pipeline
# .github/workflows/optimize-svgs.yml
- name: Optimize SVGs
run: |
node -e "
const { optimizeBatch } = require('@svg.dog/svgdog');
const fs = require('fs');
const glob = require('glob');
const files = glob.sync('src/**/*.svg').map(p => ({
name: p,
data: fs.readFileSync(p),
}));
optimizeBatch(files, { apiKey: process.env.SVGDOG_API_KEY })
.then(results => results.forEach(r => {
if (r.success) fs.writeFileSync(r.filename, r.optimized);
}));
"
env:
SVGDOG_API_KEY: ${{ secrets.SVGDOG_API_KEY }}API Reference
optimize(file, options)
Optimize a single SVG.
import { optimize } from '@svg.dog/svgdog';
const result = await optimize(buffer, { apiKey: 'YOUR_KEY' });Parameters
file—Buffer | string— SVG contentoptions.apiKey—string— Your svg.dog API key
Returns Promise<OptimizeResult>
{
svg: string; // Optimized SVG as string
originalSize: number; // Bytes before
optimizedSize: number; // Bytes after
savingsPercent: number;// 0–100
cached: boolean;
}optimizeUrl(url, options)
Fetch and optimize an SVG from a public URL.
import { optimizeUrl } from '@svg.dog/svgdog';
const result = await optimizeUrl('https://example.com/logo.svg', { apiKey: 'YOUR_KEY' });optimizeBatch(files, options)
Optimize multiple SVGs in one request. Free plan: up to 10 files. Pro: up to 50.
import { optimizeBatch } from '@svg.dog/svgdog';
import fs from 'node:fs';
const files = ['icon.svg', 'logo.svg'].map(name => ({
name,
data: fs.readFileSync(name),
}));
const results = await optimizeBatch(files, { apiKey: 'YOUR_KEY' });
results.forEach(r => {
if (r.success) {
fs.writeFileSync(r.filename, r.optimized!);
console.log(`${r.filename}: saved ${r.savingsPercent?.toFixed(1)}%`);
} else {
console.error(`${r.filename}: ${r.error}`);
}
});getUsage(options)
Get your current monthly usage.
import { getUsage } from '@svg.dog/svgdog';
const stats = await getUsage({ apiKey: 'YOUR_KEY' });
console.log(`${stats.optimizations.total} optimizations used this month`);SvgDog class
For repeated calls with the same key:
import { SvgDog } from '@svg.dog/svgdog';
const client = new SvgDog(process.env.SVGDOG_API_KEY!);
const result = await client.optimize(svgBuffer);
const stats = await client.getUsage();Error handling
All functions throw a standard Error with an additional status field for the HTTP status code:
try {
const result = await optimize(buffer, { apiKey: 'YOUR_KEY' });
} catch (err: any) {
if (err.status === 429) {
console.error('Rate limit reached — upgrade at https://svg.dog/#pricing');
} else {
console.error(err.message);
}
}Pricing
| Plan | Price | Quota | |---|---|---| | Free | €0 | 500 optimizations/month | | Pay-per-Use | €0.005/optimization | Unlimited (first 500 free) | | Volume | €0.001/optimization | After 10,000/month |
View full pricing at svg.dog →
License
MIT — svg.dog is operated by Fasterminds GmbH
