-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_link.py
More file actions
245 lines (185 loc) · 7.49 KB
/
create_link.py
File metadata and controls
245 lines (185 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
Create Link Example
This example demonstrates how to create shortened links with various options.
Use Cases:
- Create short links for marketing campaigns
- Generate trackable links for email campaigns
- Create QR codes for print materials
- Build dynamic links with custom domains
- Tag and organize links for analytics
"""
import os
from linkbreakers import (
Configuration,
ApiClient,
LinksApi,
CreateLinkRequest,
CreateBulkLinksRequest
)
def create_basic_link():
"""Example 1: Create a basic shortened link"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
links_api = LinksApi(api_client)
try:
response = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com/my-landing-page',
name='Landing Page Campaign'
)
)
print('✓ Basic link created')
print(f' - Short URL: {response.link.shortlink}')
print(f' - Link ID: {response.link.id}')
print(f' - Destination: {response.link.destination}')
return response.link
except Exception as error:
print(f'✗ Failed to create link: {error}')
raise
def create_custom_link():
"""Example 2: Create a link with custom shortlink and tags"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
links_api = LinksApi(api_client)
try:
response = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com/summer-sale',
name='Summer Sale 2024',
# Custom shortlink (must be unique in your workspace)
shortlink='summer2024',
# Tags for organization and filtering
tags=['campaign', 'summer', '2024', 'email'],
# Metadata for custom tracking (key-value pairs)
metadata={
'campaign_id': 'SUMMER_2024',
'utm_source': 'email',
'utm_medium': 'newsletter',
'utm_campaign': 'summer_sale'
}
)
)
print('✓ Custom link created')
print(f' - Short URL: {response.link.shortlink}')
print(f' - Tags: {response.link.tags}')
print(f' - Metadata: {response.link.metadata}')
return response.link
except Exception as error:
print(f'✗ Failed to create custom link: {error}')
raise
def create_link_with_qr_code():
"""Example 3: Create a link with QR code"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
links_api = LinksApi(api_client)
try:
response = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com/event-registration',
name='Event Registration QR Code',
# Wait for QR code to be generated before returning
wait_for_qrcode=True,
# Optional: Use a specific QR code template
# qrcode_template_id='your-template-uuid',
tags=['event', 'qr-code']
)
)
print('✓ Link with QR code created')
print(f' - Short URL: {response.link.shortlink}')
print(f' - QR Code URL: {response.link.qrcode_signed_url}')
print(f' - QR Code Design ID: {response.link.qrcode_design_id}')
return response.link
except Exception as error:
print(f'✗ Failed to create link with QR code: {error}')
raise
def create_link_with_custom_domain():
"""Example 4: Create a link with custom domain"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
links_api = LinksApi(api_client)
try:
response = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com/branded-content',
name='Branded Link',
# Use your custom domain (must be set up in Linkbreakers)
custom_domain_id='your-custom-domain-uuid',
shortlink='branded',
tags=['branded', 'custom-domain']
)
)
print('✓ Branded link created')
print(f' - Short URL: {response.link.shortlink}')
print(f' - Custom Domain ID: {response.link.custom_domain_id}')
return response.link
except Exception as error:
print(f'✗ Failed to create branded link: {error}')
raise
def bulk_create_links():
"""Example 5: Bulk create multiple links"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
links_api = LinksApi(api_client)
links_to_create = [
CreateLinkRequest(
destination='https://example.com/product-1',
name='Product 1',
tags=['product', 'catalog'],
metadata={'sku': 'PROD-001'}
),
CreateLinkRequest(
destination='https://example.com/product-2',
name='Product 2',
tags=['product', 'catalog'],
metadata={'sku': 'PROD-002'}
),
CreateLinkRequest(
destination='https://example.com/product-3',
name='Product 3',
tags=['product', 'catalog'],
metadata={'sku': 'PROD-003'}
)
]
try:
response = links_api.links_service_create_bulk(
create_bulk_links_request=CreateBulkLinksRequest(
links=links_to_create
)
)
print(f'✓ Bulk created {len(response.links)} links')
for link in response.links:
print(f' - {link.name}: {link.shortlink}')
return response.links
except Exception as error:
print(f'✗ Failed to bulk create links: {error}')
raise
if __name__ == '__main__':
try:
print('=== Create Link Examples ===\n')
print('1. Basic Link:')
create_basic_link()
print('\n2. Custom Link with Tags:')
create_custom_link()
print('\n3. Link with QR Code:')
create_link_with_qr_code()
print('\n4. Bulk Create Links:')
bulk_create_links()
print('\n✓ All examples completed successfully')
except Exception as error:
print(f'\n✗ Examples failed: {error}')
exit(1)