Skip to content

Node.js SDK

Use URLPix from Node.js with the built-in fetch API (Node 18+).

TIP

There is no dedicated URLPix npm package yet. Use fetch directly — the API is simple enough that a wrapper isn't needed.

Screenshot

javascript
import fs from 'fs'

async function screenshot(url, options = {}) {
  const params = new URLSearchParams({
    url,
    format: options.format || 'png',
    width: String(options.width || 1280),
    height: String(options.height || 800),
    ...(options.quality && { quality: String(options.quality) }),
    ...(options.fullPage && { fullPage: 'true' }),
    ...(options.deviceScale && { deviceScale: String(options.deviceScale) }),
  })

  const response = await fetch(
    `https://api.urlpix.com/v1/screenshot?${params}`,
    { headers: { 'X-API-Key': process.env.URLPIX_API_KEY } }
  )

  if (!response.ok) {
    throw new Error(`URLPix error: ${response.status} ${await response.text()}`)
  }

  return Buffer.from(await response.arrayBuffer())
}

// Usage
const image = await screenshot('https://example.com', {
  format: 'webp',
  width: 1920,
  height: 1080,
  quality: 90,
})
fs.writeFileSync('screenshot.webp', image)

OG Image

javascript
async function ogImage(template, variables = {}, options = {}) {
  const params = new URLSearchParams({
    template,
    format: options.format || 'png',
    ...variables,
  })

  const response = await fetch(
    `https://api.urlpix.com/v1/og?${params}`,
    { headers: { 'X-API-Key': process.env.URLPIX_API_KEY } }
  )

  if (!response.ok) {
    throw new Error(`URLPix error: ${response.status} ${await response.text()}`)
  }

  return Buffer.from(await response.arrayBuffer())
}

// Usage
const og = await ogImage('blog-post', {
  title: 'My Blog Post',
  author: 'Jane Doe',
})
fs.writeFileSync('og.png', og)

Express Middleware Example

javascript
import express from 'express'
const app = express()

app.get('/og', async (req, res) => {
  const { title, template = 'default' } = req.query

  const params = new URLSearchParams({ template, title })
  const response = await fetch(
    `https://api.urlpix.com/v1/og?${params}`,
    { headers: { 'X-API-Key': process.env.URLPIX_API_KEY } }
  )

  res.set('Content-Type', response.headers.get('content-type'))
  res.set('Cache-Control', 'public, max-age=86400')
  res.send(Buffer.from(await response.arrayBuffer()))
})

URLPix — Screenshot & OG Image API