Deep links from a Next.js site into your app
Your Next.js site is where many app journeys start: a product page, a share button, a sign-up flow. This guide covers creating links on the server, opening the app from the site, and counting the visits and sales a tap brings to your site.
- Guide
- Web
- Android
- iPhone
@straitlink/node- Faster: one prompt
Where a Next.js site fits
A link points at a web address, for example https://shop.example.com/products/42 on your Next.js site. The link server decides per tap: a computer goes to that page, a phone with your app opens the matching app screen, and a phone without it goes to the store with the link kept for after the install. Your site doesn't need to know any of this. It just needs to hand out links instead of raw URLs where an app open is worth more than a page view.
None of it depends on how you render. Server components, static export and client components all work, because the link server sits in front of the tap, not inside your app.
Create links in a Route Handler
Link creation needs your secret key, so it belongs on the server. A Route Handler keeps the key in an environment variable and returns only the short link:
import { StraitClient } from '@straitlink/node';
const strait = new StraitClient({ apiKey: process.env.STRAIT_SECRET_KEY!, baseUrl: 'https://strait.link' });
export async function POST(req: Request) {
const { productId } = await req.json();
const link = await strait.createLink({
slug: `p-${productId}-${crypto.randomUUID().slice(0, 6)}`,
longUrl: `https://shop.example.com/products/${productId}`,
title: 'Look at this',
tags: ['web-share'],
});
return Response.json({ url: `https://hilltop.strait.link/${link.slug}` }); // your workspace's link domain
}Never prefix the key with NEXT_PUBLIC_: that ships it to every browser. If you only need a few fixed links (a footer "Get the app", a campaign landing page), create them once in the dashboard and paste them in.
An "Open in app" button
On a product page, a button that points at that product's link gives mobile visitors a one-tap way into the app, while desktop visitors stay on the page. Inside Instagram or WhatsApp, where many of your visitors arrive, the link server shows its Open in app page instead of a redirect that browser may not pass to your app.
Count the visit and credit the sale
A deferred deep link resolves inside your app (the iOS, Android, React Native, Flutter and Capacitor SDKs), not on a website: a tap that goes to your Next.js site already opens the link's destination URL, so your routes see its path and query as usual. What @straitlink/web adds on the web is the tap: reportWebOpen counts the visit as an open of the tap that brought it, and trackEvent joins a signup or purchase to that tap (web SDK). Run it in a client component, once:
'use client';
import { useEffect } from 'react';
import { reportWebOpen } from '@straitlink/web';
export function StraitOpen() {
useEffect(() => {
reportWebOpen({ publishableKey: 'st_pub_live_…', endpoint: 'https://hilltop.strait.link' });
}, []);
return null;
}The publishable key is safe in the browser. The call never throws, and it reports only a visit whose URL carries strait_click (a landing from a tap); a reload of that URL counts once. Later, trackEvent(config, 'purchase', { value, currency, eventId: order.id }) attaches the same tap; send revenue you rely on from your server with the Node SDK.
Your own domain opening the app
The link domain already serves assetlinks.json and apple-app-site-association for your app. If you also want plain URLs on your Next.js domain to open the app, put both files in public/.well-known/ (the free generators write them) and add a headers() rule in next.config.js that serves the Apple file as application/json, since it has no extension.
Docs for this setup
Where to go next for Next.js
Check your setup
Free tools for the parts that usually break
Other guides: Expo · FlutterFlow · Capacitor and Ionic · Jetpack Compose (Kotlin) · SwiftUI · Unity
Ship links that open your Next.js app.
Free for 25,000 taps a month, no card. Every tap gets a trace you can read.