npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ohimg/ohimg-js

v1.1.4

Published

JavaScript SDK for OhImg - Open Graph Image Generator

Downloads

799

Readme

ohimg-js

JavaScript/TypeScript SDK for OhImg - Open Graph Image Generator. Generate secure, signed Open Graph images for your website.

Features

  • 🔒 Secure URL generation with HMAC signatures
  • 🌐 Works everywhere (Node.js, Browsers, Deno, Cloudflare Workers)
  • 📦 Zero dependencies
  • 💪 Full TypeScript support
  • ⚡ Async/await API

Installation

npm install ohimg-js
# or
yarn add ohimg-js
# or
pnpm add ohimg-js

Quick Start

import { OhImg } from "ohimg-js";

const ohimg = new OhImg({
  publishableKey: "omg_pub_xxxx", // Your OhImg API key
  signatureSecret: "omg_whsec_xxxx", // Your OhImg Signature secret
});

const ogImageUrl = await ohimg.getImageUrl({
  path: "/blog/my-post",
  domain: "https://myblog.com",
});

Framework Examples

Next.js

// pages/blog/[slug].tsx
import { OhImg } from "ohimg-js";

export default function BlogPost({ ogImageUrl }) {
  return (
    <Head>
      <meta property="og:image" content={ogImageUrl} />
      <meta property="twitter:image" content={ogImageUrl} />
    </Head>
  );
}

export async function getStaticProps({ params }) {
  const ohimg = new OhImg({
    publishableKey: process.env.OHIMG_API_KEY!,
    signatureSecret: process.env.OHIMG_WEBHOOK_SECRET!,
  });

  const ogImageUrl = await ohimg.getImageUrl({
    path: `/blog/${params.slug}`,
    domain: process.env.NEXT_PUBLIC_DOMAIN!,
  });

  return {
    props: { ogImageUrl },
  };
}

Astro

---
import { OhImg } from 'ohimg-js';

const ohimg = new OhImg({
  publishableKey: import.meta.env.OHIMG_API_KEY,
  signatureSecret: import.meta.env.OHIMG_WEBHOOK_SECRET
});

const ogImageUrl = await ohimg.getImageUrl({
  path: Astro.url.pathname,
  domain: import.meta.env.SITE
});
---

<meta property="og:image" content={ogImageUrl} />

API Reference

Configuration

const ohimg = new OhImg({
  publishableKey: string;        // Required: Your OhImg API key
  signatureSecret: string; // Required: Your OhImg Signature secret
  baseUrl?: string;      // Optional: Custom base URL (default: https://og.ohimg.dev)
});

Methods

getImageUrl(input)

Generate a complete OG image URL.

const url = await ohimg.getImageUrl({
  path: string;   // Required: Path of the page (e.g., '/blog/post')
  domain: string; // Required: Full domain with protocol (e.g., 'https://example.com')
});

generateSignature(input)

Generate signature components separately.

const {
  signature,  // Generated HMAC signature
  fullUrl     // Complete URL
} = await ohimg.generateSignature({
  path: string;   // Required: Path of the page
  domain: string; // Required: Full domain with protocol
});

Helper Function

For one-off usage:

import { getOGImageUrl } from "ohimg-js";

const ogImageUrl = await getOGImageUrl(
  {
    publishableKey: "omg_pub_xxxx",
    signatureSecret: "omg_whsec_xxxx",
  },
  {
    path: "/about",
    domain: "https://myblog.com",
  }
);

Environment Variables

OHIMG_API_KEY=omg_pub_xxxx
OHIMG_WEBHOOK_SECRET=omg_whsec_xxxx

Security

This SDK uses HMAC-SHA256 for request signing with these security features:

  • Domain validation
  • URL tampering prevention
  • Protocol enforcement

Troubleshooting

Common errors:

// Domain must include protocol
❌ domain: 'myblog.com'
✅ domain: 'https://myblog.com'

// Path must start with /
❌ path: 'blog/post'
✅ path: '/blog/post'

// API key and Signature secret required
❌ new OhImg({ publishableKey: '' })
✅ new OhImg({ publishableKey: 'omg_pub_xxx', signatureSecret: 'omg_whsec_xxx' })

License

MIT

Support