Angular applications often need to generate screenshots of web pages: dashboards that export PDF reports, e-commerce platforms capturing product page previews, or SaaS tools archiving customer-facing views. Building a headless browser in-house costs weeks of engineering time. SnapAPI gives your Angular app a single REST endpoint that returns a screenshot in under two seconds.
This guide covers integrating SnapAPI into an Angular service, handling async screenshot requests, displaying results in your component, and best practices for caching and error handling in production.
Setting Up the SnapAPI Angular Service
Start by generating a dedicated service to encapsulate all screenshot API calls. Angular services keep your component logic clean and make the screenshot functionality reusable across modules.
ng generate service screenshot
Inside screenshot.service.ts, inject HttpClient and define the base URL:
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class ScreenshotService {
private API = 'https://snapapi.pics/screenshot';
private KEY = 'YOUR_ACCESS_KEY';
constructor(private http: HttpClient) {}
capture(url: string, width = 1280): Observable<Blob> {
const params = new HttpParams()
.set('access_key', this.KEY)
.set('url', url)
.set('viewport_width', width);
return this.http.get(this.API, { params, responseType: 'blob' });
}
}
Displaying Screenshots in a Component
With the service ready, inject it into any component that needs screenshot functionality. Use Angular's DomSanitizer to safely convert the Blob response into an object URL that can be bound to an <img> tag.
import { Component } from '@angular/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { ScreenshotService } from './screenshot.service';
@Component({
selector: 'app-preview',
template: `
<input [(ngModel)]="targetUrl" placeholder="Enter URL" />
<button (click)="capture()">Capture</button>
<img *ngIf="previewUrl" [src]="previewUrl" />
`
})
export class PreviewComponent {
targetUrl = '';
previewUrl: SafeUrl | null = null;
constructor(
private svc: ScreenshotService,
private sanitizer: DomSanitizer
) {}
capture() {
this.svc.capture(this.targetUrl).subscribe(blob => {
const objUrl = URL.createObjectURL(blob);
this.previewUrl = this.sanitizer.bypassSecurityTrustUrl(objUrl);
});
}
}
Full-Page Screenshots and Custom Viewports
SnapAPI supports full-page screenshots that capture content below the fold. Pass full_page=true as a query parameter to capture the entire scrollable height. This is essential for archiving long-form landing pages or terms-of-service documents. For mobile previews, set viewport_width=375 and device_scale_factor=2 to simulate a high-DPI iPhone screen. Angular services make it trivial to parameterize these options per component call.
Caching Screenshots with Angular HTTP Interceptors
Screenshots of stable pages do not need to be recaptured on every view. An Angular HTTP interceptor can cache responses by URL and return the cached Blob on repeated requests. Store cache entries with a timestamp and invalidate after your desired TTL, typically 24 hours for docs pages. SnapAPI also supports a server-side cache_ttl parameter measured in seconds, so you can control freshness at the API level without managing it in your Angular code. Combining both approaches minimizes API calls and keeps your UI snappy.
Error Handling and Retry Logic
Network errors and timeout responses need graceful handling in production Angular apps. Use RxJS catchError and retry operators in your service to automatically retry failed requests up to three times with exponential backoff. SnapAPI returns standard HTTP status codes: 200 for success, 422 for invalid parameters, 429 for rate limit exceeded, and 503 for temporary rendering failures. Display a placeholder image in your component while the retry is in flight. Log errors to your monitoring system with the SnapAPI request ID included in the response headers.
Generating PDF Reports from Angular Dashboards
Angular dashboard applications frequently need to export current view as a PDF report. SnapAPI's PDF endpoint renders any URL to a full PDF document in one API call. Pass your dashboard URL with an auth header, set pdf_format=A4 and pdf_landscape=true, and receive a binary PDF ready for download. Trigger the capture from a button click event in your Angular component and use FileSaver.js or the browser's native download API to save the result. No server-side PDF generation library required.
Open Graph Image Generation for Angular SSR Apps
If you run Angular Universal for server-side rendering, you can generate OG images dynamically at build time or on first request. Call SnapAPI from your Node.js SSR server when a new page is published, save the image to CDN storage, and embed the URL in your page's <meta property="og:image"> tag. Social platforms pick up the cached OG image immediately. This pattern works with any Angular SSR setup including NestJS backends and Express adapters.
Getting Started
Create a free SnapAPI account at snapapi.pics/dashboard. Your API key is available immediately after signup. The free plan includes 200 screenshots per month with no credit card required. Read the full parameter reference at snapapi.pics/docs to explore advanced options including element selectors, CSS injection, proxy support, and webhook callbacks.