Create links from a Flutter app
After this page, a Share button in your Flutter 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. The app asks your server; your server calls POST /v1/links.
1. Add an endpoint to your server
Use the server code from the React Native page (Node), or call the API from any language:
curl https://strait.link/v1/links \
-H "Authorization: Bearer $SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{ "slug": "p42-7f3e2c1a", "longUrl": "https://shop.example.com/product/42?ref=user_123", "campaign": "in_app_share" }'Slugs are unique within your workspace, so your server should add a random part. Reply to the app with the shortUrl from the answer.
You should see: a 201 answer with "shortUrl": "https://hilltop.strait.link/p42-7f3e2c1a".
2. Call it from the app and share
flutter pub add http share_plusimport 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:share_plus/share_plus.dart';
class ShareButton extends StatelessWidget {
const ShareButton({super.key, required this.productId});
final String productId;
Future<void> _share() async {
final res = await http.post(
Uri.parse('https://api.yourapp.com/share'), // your server, with your own auth
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'productId': productId}),
);
final url = (jsonDecode(res.body) as Map<String, dynamic>)['url'] as String;
await Share.share(url);
}
@override
Widget build(BuildContext context) => TextButton(onPressed: _share, child: const Text('Share'));
}You should see: the share sheet with a https://hilltop.strait.link/… link. Tap it on another phone: the app opens on that product.
3. Read the referrer when the link opens the app
links.onLink.listen((e) {
final ref = e.params?['ref'];
if (e.matched && ref != null) saveReferrer(ref); // your own code
});You should see: ref set to the sharer's id when a friend opens the link, including after installing from the store.
Common errors
401from Strait. Your server sent the publishable key. Creating links needs a secret key.409. The slug is already used in your workspace. Make the random part longer.402. The workspace reached its plan's link limit. Existing links keep working.
Next steps
- Events & revenue: see which shares led to purchases.
- Create a link (API): every field, including fallbacks, previews and expiry.