# Next.js deep linking to your mobile app

> How to use deep links with a Next.js website (App Router), create links from a Route Handler with a secret key, add an Open in app button, count tap visits and sales in a client component, and serve App Links files from public/.well-known.

Source: https://straitlink.in/guides/nextjs-deep-linking/
Markdown: https://straitlink.in/guides/nextjs-deep-linking.md

- [Guides](https://straitlink.in/guides/)
- Next.js

# 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](https://straitlink.in/ai/#prompt-server)

STEPS · 5

- [1Where a Next.js site fits](https://straitlink.in/guides/nextjs-deep-linking/#where-a-nextjs-site-fits)
- [2Create links in a Route Handler](https://straitlink.in/guides/nextjs-deep-linking/#create-links-in-a-route-handler)
- [3An "Open in app" button](https://straitlink.in/guides/nextjs-deep-linking/#an-open-in-app-button)
- [4Count the visit and credit the sale](https://straitlink.in/guides/nextjs-deep-linking/#count-the-visit-and-credit-the-sale)
- [5Your own domain opening the app](https://straitlink.in/guides/nextjs-deep-linking/#your-own-domain-opening-the-app)

**Step 1 of 5**Where a Next.js site fits

AT A GLANCE · NEXT.JS

- Builds on : `@straitlink/node`

- Platforms : Web, with links into Android and iPhone apps

- Start in the docs : [Node SDK](https://straitlink.in/docs/sdks/node/)

- Free plan : 25,000 taps a month, no card

## 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](https://straitlink.in/docs/sdks/web/#deferred-deep-links)). 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.

[Open the Next.js docs](https://straitlink.in/docs/sdks/node/)

Docs for this setup

## Where to go next for Next.js

- [Node SDK Create links and send events from your server](https://straitlink.in/docs/sdks/node/)
- [Web SDK Count tap visits and sales on your website](https://straitlink.in/docs/sdks/web/)
- [Create a link (API) Every field a link can have](https://straitlink.in/docs/api/create-link/)

Check your setup

## Free tools for the parts that usually break

- [Will it open my app? Simulate a tap from WhatsApp, Instagram or Chrome](https://straitlink.in/tools/will-it-open/)
- [assetlinks.json checker Is your Android App Links file right?](https://straitlink.in/tools/assetlinks-checker/)
- [AASA validator Is your apple-app-site-association file right?](https://straitlink.in/tools/aasa-validator/)
- [Troubleshooting When a link opens the browser instead of the app](https://straitlink.in/docs/troubleshooting/)

Other guides: [Expo](https://straitlink.in/guides/expo-deep-linking/) · [FlutterFlow](https://straitlink.in/guides/flutterflow-deep-linking/) · [Capacitor and Ionic](https://straitlink.in/guides/capacitor-deep-linking/) · [Jetpack Compose (Kotlin)](https://straitlink.in/guides/jetpack-compose-deep-linking/) · [SwiftUI](https://straitlink.in/guides/swiftui-deep-linking/) · [Unity](https://straitlink.in/guides/unity-deep-linking/)

## 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.

[Start free](https://app.straitlink.in/?signup=1) [Read the quick start](https://straitlink.in/docs/quick-start/)
