Create links from a React Native app
After this page, a Share button in your app creates a Strait link for the current screen and opens the system share sheet.
On this page 5
- Time
- About 15 minutes
- You need
- A backend you control, a secret key (Dashboard → Settings → Keys)
- Works on
- Android, iPhone
Links are created with the secret key, which must never ship in an app. So the app asks your server, and your server asks Strait. The same pattern works for referral links: put the referrer in the destination's query, and it comes back in e.params when the link opens the app.
1. Add an endpoint to your server
With the Node SDK (any language can call POST /v1/links instead):
import { randomBytes } from 'node:crypto';
import express from 'express';
import { StraitClient } from '@straitlink/node';
const strait = new StraitClient({ apiKey: process.env.SECRET_KEY!, baseUrl: 'https://strait.link' });
const app = express();
app.use(express.json());
// POST /share { "productId": "42" } → { "url": "https://hilltop.strait.link/…" }
app.post('/share', async (req, res) => {
const id = String(req.body.productId).replace(/[^\w-]/g, '');
const link = await strait.createLink({
slug: `p${id}-${randomBytes(4).toString('hex')}`, // unique in your workspace
longUrl: `https://shop.example.com/product/${id}?ref=${encodeURIComponent(req.user?.id ?? 'guest')}`,
title: 'Look what I found', // preview in WhatsApp and other chat apps
campaign: 'in_app_share',
});
res.json({ url: link.shortUrl });
});Slugs are unique within your workspace (letters, digits, - and _, up to 64), so add a random part. Check who's calling before you create links for them, as you would for any write.
You should see: curl -X POST localhost:3000/share -H 'Content-Type: application/json' -d '{"productId":"42"}' returns {"url":"https://hilltop.strait.link/…"}.
2. Call it from the app and share
import { Button, Share } from 'react-native';
export function ShareButton({ productId }: { productId: string }) {
async function share() {
const res = await fetch('https://api.yourapp.com/share', {
method: 'POST',
headers: { 'Content-Type': 'application/json' }, // add your own auth header
body: JSON.stringify({ productId }),
});
const { url } = await res.json();
await Share.share({ message: url });
}
return <Button title="Share" onPress={share} />;
}You should see: the system share sheet with a https://hilltop.strait.link/… link. Send it to yourself and tap it: the app opens on that product.
3. Read the referrer when the link opens the app
The destination's query arrives in e.params, on a direct open and on the first open after an install:
links.onLink((e) => {
if (e.matched && e.params?.ref) saveReferrer(e.params.ref); // your own code
});You should see: e.params.ref set to the sharer's id when a friend opens the shared link, including after installing from the store.
Common errors
401from Strait. Your server sent the publishable key. Link creation needs a secret key.409. Thatslugis already used in your workspace. Make the random part longer.402. The workspace reached its plan's link limit. Existing links keep working. See Pricing.
Next steps
- Events & revenue: see which shares led to purchases.
- Create a link (API): every field, including fallbacks, previews and expiry.