Skip to main content
RapidDev - Software Development Agency
lovable-integrationsEdge Function Integration

How to Integrate Lovable with DHL Express API

To integrate the DHL Express API with Lovable, create a Supabase Edge Function that proxies rating, shipment creation, and tracking requests using your DHL API key and account number stored in Cloud Secrets. This lets you build international shipping calculators, label generators, and parcel trackers in Lovable without exposing DHL credentials to the browser. Setup takes about 45 minutes.

What you'll learn

  • How to obtain a DHL Express API key and account number from the DHL Developer Portal
  • How to store DHL credentials securely in Lovable Cloud Secrets
  • How to write a Supabase Edge Function that calls the DHL Rate, Shipment, and Tracking APIs on Deno
  • How to build an international shipping calculator UI in React that fetches live DHL rates
  • How to handle DHL account number requirements and international address validation in your app
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Intermediate15 min read45 minutesE-commerceMarch 2026RapidDev Engineering Team
TL;DR

To integrate the DHL Express API with Lovable, create a Supabase Edge Function that proxies rating, shipment creation, and tracking requests using your DHL API key and account number stored in Cloud Secrets. This lets you build international shipping calculators, label generators, and parcel trackers in Lovable without exposing DHL credentials to the browser. Setup takes about 45 minutes.

Add International Shipping Rates and Tracking to Your Lovable App with DHL

DHL Express is the global standard for international express shipping — if your e-commerce app ships across borders, DHL's APIs give you real-time rate quotes, automated label generation, and end-to-end parcel tracking across 220+ countries and territories. Integrating DHL with Lovable means your customers see accurate shipping costs at checkout, you can print DHL labels from within your app, and order tracking pages update automatically as packages move through DHL's network.

Because DHL's API requires authentication via an API key tied to a business account number, all calls must flow through Lovable's server-side Edge Function layer. Your DHL API key and account number live encrypted in Cloud → Secrets — they never touch the browser. The Edge Function acts as a secure proxy: it receives a request from your React frontend (e.g., 'get rates for a 2kg package from Berlin to Tokyo'), authenticates with DHL's REST API, and returns rate options or tracking status back to the UI.

DHL Express differs from domestic carriers like UPS in its international specialist focus. The DHL API is purpose-built for cross-border shipments with features like commodity declarations, customs duty estimation, and multi-piece shipment support — capabilities that matter when you're building a storefront that sells internationally. This guide covers the three DHL API products you'll use most: the Rate API for cost calculations, the Shipment API for label generation, and the Tracking API for delivery updates.

Integration method

Edge Function Integration

DHL Express API integration in Lovable works by routing all API calls through Supabase Edge Functions running on Deno. Your DHL API key and account number are stored as encrypted secrets in Cloud → Secrets and accessed via Deno.env.get() inside the Edge Function — never exposed to the browser. The React frontend calls the Edge Function endpoint, which authenticates with DHL and proxies rating, shipment, and tracking requests server-side.

Prerequisites

  • A DHL Express business account — register at developer.dhl.com and request API access (DHL provides a sandbox environment for testing)
  • Your DHL API key and DHL account number from the DHL Developer Portal dashboard
  • A Lovable project with Lovable Cloud enabled (the default for all new projects)
  • Basic understanding of what an Edge Function does — it runs server-side TypeScript on Deno, separate from your React frontend
  • An e-commerce or logistics app in Lovable that needs shipping rate, label, or tracking functionality

Step-by-step guide

1

Get your DHL API credentials from the Developer Portal

Before writing any code, you need a DHL API key and account number. Navigate to developer.dhl.com and sign in or create a free developer account. Once logged in, go to My Apps and click Create App. Give the app a name (e.g., 'Lovable Storefront') and select the APIs you want access to — at minimum, select Express Rating, Express Shipment, and Express Tracking. After creating the app, DHL generates an API key (a long alphanumeric string) visible on the app detail page. Importantly, DHL Express APIs also require your DHL account number — this is the 9-digit number associated with your DHL Express business account, not your developer portal account. If you don't have a DHL business account yet, contact DHL sales to open one; the developer sandbox works with a test account number provided in the developer portal documentation. Write down both your API key and account number before proceeding — you'll need both for the Cloud Secrets step. DHL's sandbox environment uses the base URL https://api-mock.dhl.com/mydhlapi for testing. Production uses https://api.dhl.com/mydhlapi. It is strongly recommended to build and test with the sandbox first, then swap to production credentials when you're ready to go live.

Pro tip: DHL's sandbox provides mock responses for all API endpoints — use the sandbox account number '123456789' listed in their developer docs during development so you don't consume real shipment credits.

Expected result: You have a DHL API key (e.g., 'a1b2c3d4e5f6...') and a 9-digit DHL account number ready to paste into Cloud Secrets.

2

Store DHL credentials in Cloud Secrets

Now that you have your DHL API key and account number, store them securely in Lovable's Cloud Secrets panel. In your Lovable project, look at the top of the screen and click the '+' icon next to the Preview panel to open the Cloud tab. Inside the Cloud tab, click Secrets in the left-hand navigation. You'll see a list of encrypted environment variables — click Add Secret to add each credential. Add your first secret: set the name to DHL_API_KEY and paste your DHL API key as the value, then click Save. Add a second secret: set the name to DHL_ACCOUNT_NUMBER and enter your 9-digit account number as the value, then click Save. The secrets are now encrypted and stored — they will only be accessible from Edge Functions via Deno.env.get(), never from frontend React code or the browser. A critical rule: never paste your DHL API key directly into Lovable's chat prompt or into your React component code. On Lovable's free tier, chat history is publicly visible, and keys pasted in chat are recoverable from Git commit history. Always use Cloud → Secrets for any credential that authenticates an external API. Lovable's security layer blocks approximately 1,200 hardcoded API keys per day — use the Secrets panel to stay on the right side of that protection.

Pro tip: Name your secrets with clear prefixes like DHL_ so they're easy to identify later when you have multiple API credentials in the Secrets panel.

Expected result: The Cloud → Secrets panel shows two entries: DHL_API_KEY and DHL_ACCOUNT_NUMBER, both displaying as encrypted values.

3

Create the DHL Edge Function proxy

With credentials secured, ask Lovable to create the Edge Function that proxies DHL API calls. Type the following prompt into Lovable's chat interface. Lovable will generate the TypeScript code, create the file at supabase/functions/dhl-proxy/index.ts, and deploy it to your Cloud project automatically. The Edge Function uses a switch on an 'action' query parameter to route between the three DHL API endpoints: 'rates' calls the DHL Rate API, 'shipment' calls the Shipment API, and 'track' calls the Tracking API. The DHL Express REST API uses HTTP Basic Auth where the username is your account number and the password is your API key. The function constructs the Authorization header using btoa() to base64-encode the credentials retrieved from Deno.env.get(). All responses include CORS headers so your React frontend can call the function from the browser.

Lovable Prompt

Create a Supabase Edge Function at supabase/functions/dhl-proxy/index.ts that proxies requests to the DHL Express API. Use Deno.env.get('DHL_API_KEY') and Deno.env.get('DHL_ACCOUNT_NUMBER') for credentials. The function should handle an 'action' query parameter with three values: 'rates' (POST to https://api-mock.dhl.com/mydhlapi/rates), 'shipment' (POST to https://api-mock.dhl.com/mydhlapi/shipments), and 'track' (GET to https://api-mock.dhl.com/mydhlapi/tracking?trackingNumber={id}). Use HTTP Basic Auth with the account number as username and API key as password. Include CORS headers in all responses. Return errors from DHL with their original status code.

Paste this in Lovable chat

supabase/functions/dhl-proxy/index.ts
1import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
2
3const DHL_BASE_URL = 'https://api-mock.dhl.com/mydhlapi'
4
5const corsHeaders = {
6 'Access-Control-Allow-Origin': '*',
7 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
8}
9
10serve(async (req) => {
11 if (req.method === 'OPTIONS') {
12 return new Response('ok', { headers: corsHeaders })
13 }
14
15 try {
16 const apiKey = Deno.env.get('DHL_API_KEY')
17 const accountNumber = Deno.env.get('DHL_ACCOUNT_NUMBER')
18
19 if (!apiKey || !accountNumber) {
20 return new Response(
21 JSON.stringify({ error: 'DHL credentials not configured in Cloud Secrets' }),
22 { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
23 )
24 }
25
26 const authHeader = 'Basic ' + btoa(`${accountNumber}:${apiKey}`)
27 const url = new URL(req.url)
28 const action = url.searchParams.get('action')
29
30 let dhlUrl: string
31 let dhlMethod: string
32 let dhlBody: string | undefined
33
34 if (action === 'rates') {
35 dhlUrl = `${DHL_BASE_URL}/rates`
36 dhlMethod = 'POST'
37 dhlBody = await req.text()
38 } else if (action === 'shipment') {
39 dhlUrl = `${DHL_BASE_URL}/shipments`
40 dhlMethod = 'POST'
41 dhlBody = await req.text()
42 } else if (action === 'track') {
43 const trackingNumber = url.searchParams.get('trackingNumber')
44 dhlUrl = `${DHL_BASE_URL}/tracking?trackingNumber=${trackingNumber}&trackingView=all-checkpoints&levelOfDetail=all`
45 dhlMethod = 'GET'
46 } else {
47 return new Response(
48 JSON.stringify({ error: 'Invalid action. Use: rates, shipment, or track' }),
49 { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
50 )
51 }
52
53 const dhlResponse = await fetch(dhlUrl, {
54 method: dhlMethod,
55 headers: {
56 'Authorization': authHeader,
57 'Content-Type': 'application/json',
58 'Accept': 'application/json',
59 'Message-Reference': crypto.randomUUID(),
60 'Message-Reference-Date': new Date().toISOString(),
61 'Plugin-Name': 'Lovable',
62 'Plugin-Version': '1.0',
63 'Shipper-Account': accountNumber,
64 },
65 body: dhlBody,
66 })
67
68 const data = await dhlResponse.json()
69
70 return new Response(JSON.stringify(data), {
71 status: dhlResponse.status,
72 headers: { ...corsHeaders, 'Content-Type': 'application/json' },
73 })
74 } catch (error) {
75 return new Response(
76 JSON.stringify({ error: error.message }),
77 { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
78 )
79 }
80})

Pro tip: Switch DHL_BASE_URL to 'https://api.dhl.com/mydhlapi' when you're ready to go live. Swap your Cloud Secrets to your production DHL API key and account number at the same time.

Expected result: Lovable creates the Edge Function file, deploys it to Cloud, and confirms it's live. You'll see the function listed under Cloud → Edge Functions in your project.

4

Build the shipping rate calculator React component

With the Edge Function deployed, ask Lovable to build a rate calculator UI component that calls it. This component collects shipment details from the user — origin and destination countries, package weight, and dimensions — formats them as a DHL Rate API request body, sends it to your Edge Function, and displays the returned service options with prices and delivery dates. The DHL Rate API expects a specific JSON body structure with plannedShippingDateAndTime, unitOfMeasurement, isCustomsDeclarable, productCode ('EXPRESS_WORLDWIDE' or left empty for all products), packages array with weight and dimensions, and customerDetails with shipperDetails and receiverDetails including postal code, city, and countryCode. Lovable will generate all of this from the prompt below, including a TypeScript interface for the rate response and a properly typed component using Tailwind CSS and shadcn/ui. The component calls the Edge Function at /functions/v1/dhl-proxy?action=rates using the Supabase client's functions.invoke() method or a direct fetch to the function URL. Error states are handled gracefully — if DHL returns an error (e.g., invalid account number in sandbox), the component displays the error message rather than crashing. Successful responses show a list of available DHL services with price, currency, and estimated transit days.

Lovable Prompt

Create a DHL shipping rate calculator component. Add a form with fields for: origin country code (ISO 2-letter), destination country code, package weight in kg, length/width/height in cm, and shipment date. On submit, POST to the dhl-proxy Edge Function with action=rates. The DHL Rate API body should include plannedShippingDateAndTime, unitOfMeasurement (metric), isCustomsDeclarable (false for now), and customerDetails with shipperDetails and receiverDetails using the country codes. Display each returned product (service name, total price with currency, transit days, estimated delivery date) as a card. Show a loading state while waiting and display any DHL error messages.

Paste this in Lovable chat

Pro tip: Use ISO 3166-1 alpha-2 country codes (e.g., 'DE' for Germany, 'JP' for Japan). DHL returns an error if you use full country names instead of 2-letter codes.

Expected result: A rate calculator form appears in your Lovable preview. Entering test values and clicking Calculate shows DHL service options with prices returned from the Edge Function.

5

Add parcel tracking and test end-to-end

Add a tracking page that lets customers look up a DHL shipment by tracking number. This calls the Edge Function with action=track and the tracking number as a query parameter, then displays a timeline of shipment events from DHL's Tracking API. Once both components are working, test the full flow end-to-end. For the sandbox environment, DHL provides test tracking numbers in their developer documentation (e.g., '1234567890' returns a pre-defined tracking response). Enter a test tracking number and verify the event timeline displays correctly — you should see events like 'Shipment picked up', 'Arrived at facility', and 'Delivered' with timestamps and locations. For complex integrations involving customs declarations, multi-piece shipments, or DHL account setup issues, RapidDev's team has experience configuring DHL Express integrations for international e-commerce apps and can help troubleshoot account provisioning and API authentication errors. Before going to production, switch your Cloud Secrets from sandbox values to your live DHL API key and account number, and update DHL_BASE_URL in the Edge Function from api-mock.dhl.com to api.dhl.com. Run a test rate request against the live API to confirm your production credentials work before launching.

Lovable Prompt

Add a parcel tracking page to my app. Create a form with a single text input for a DHL tracking number. On submit, call the dhl-proxy Edge Function with action=track and the tracking number as a query parameter. Display the returned events as a vertical timeline with the most recent event at the top — show each event's timestamp (formatted as local time), location description, and event description. Show a 'Tracking number not found' message if DHL returns a 404. Add this page to the navigation.

Paste this in Lovable chat

Pro tip: DHL's sandbox tracking numbers are documented at developer.dhl.com/api-reference/dhl-express. Use '1234567890' as a test tracking number to see a full mock event timeline.

Expected result: The tracking page loads, accepts a test DHL tracking number, and displays a timeline of shipment events returned from the DHL Tracking API via your Edge Function.

Common use cases

International shipping rate calculator at checkout

Display live DHL Express rate options to customers at checkout based on their destination country, package weight, and dimensions. Show estimated delivery dates alongside prices so customers can choose between DHL Express Worldwide, DHL Express 12:00, and other service levels.

Lovable Prompt

Create a shipping rate calculator component that calls an Edge Function to get DHL Express rates. The form takes origin country, destination country, weight in kg, and dimensions in cm. Display each available DHL service with its price in USD and estimated delivery date. Store my DHL API key and account number in Cloud Secrets.

Copy this prompt to try it in Lovable

DHL shipment creation and label printing

Allow store admins to generate DHL Express shipping labels directly from your Lovable app. The Edge Function calls the DHL Shipment API with sender, recipient, and package details and returns a PDF label URL for printing.

Lovable Prompt

Build an order fulfillment page where I can enter shipper and recipient addresses, package weight, and dimensions, then click Create Shipment to call the DHL Shipment API via an Edge Function. Display the returned tracking number and show a Download Label button that opens the PDF. Save the tracking number to the orders table in Supabase.

Copy this prompt to try it in Lovable

Parcel tracking status dashboard

Give customers a self-service tracking page where they enter a DHL tracking number and see real-time shipment events, current location, and estimated delivery date. The Edge Function calls DHL's Tracking API and formats the event timeline for display.

Lovable Prompt

Create a parcel tracking page where users enter a DHL tracking number and see a timeline of shipment events from the DHL Tracking API. Call the API via an Edge Function using my DHL API key in Cloud Secrets. Show each event with its timestamp, location, and description. Highlight the current status at the top.

Copy this prompt to try it in Lovable

Troubleshooting

Edge Function returns 401 Unauthorized from DHL

Cause: The DHL Basic Auth header is constructed incorrectly, or the API key / account number values in Cloud Secrets contain extra whitespace or are swapped (account number used as key and vice versa).

Solution: Go to Cloud → Secrets and verify DHL_API_KEY contains the API key string and DHL_ACCOUNT_NUMBER contains the 9-digit account number — not the other way around. The Basic Auth format is base64(accountNumber:apiKey). Also check that neither value has leading or trailing spaces, which can happen when copy-pasting. Delete and re-enter the secrets if unsure.

DHL Rate API returns 'No rates found' or an empty products array

Cause: The requested route or package dimensions are outside DHL Express service coverage, or the plannedShippingDateAndTime is in the past or uses an incorrect ISO 8601 format.

Solution: Ensure plannedShippingDateAndTime is formatted as 'YYYY-MM-DDTHH:MM:SS GMT+HH:MM' — for example '2026-04-01T14:00:00 GMT+00:00'. Verify the origin and destination country codes are valid ISO 3166-1 alpha-2 codes. Test with common routes like US→GB or DE→JP to confirm the API is responding before testing edge routes.

typescript
1// Correct date format for DHL Rate API
2const plannedDate = new Date()
3plannedDate.setDate(plannedDate.getDate() + 1) // Tomorrow
4const isoDate = plannedDate.toISOString().slice(0, 19) + ' GMT+00:00'

CORS error when calling the Edge Function from the React frontend

Cause: The Edge Function is not handling the OPTIONS preflight request, or the CORS headers are missing from error responses.

Solution: Check that the Edge Function returns 'ok' with corsHeaders for OPTIONS requests before any other logic runs. Also ensure every return statement in the function — including catch blocks and early error returns — includes the corsHeaders object spread. Rebuild the Edge Function via Lovable chat if the CORS handling is missing.

typescript
1// Add to every Response in the Edge Function
2return new Response(JSON.stringify(data), {
3 status: dhlResponse.status,
4 headers: { ...corsHeaders, 'Content-Type': 'application/json' },
5})

DHL returns 'Secret DHL_API_KEY not found' error (P0001) in Cloud Logs

Cause: The secret name referenced in Deno.env.get() does not exactly match the name stored in Cloud → Secrets, including case sensitivity.

Solution: Open Cloud → Secrets and confirm the exact secret names match what the Edge Function code uses. Secret names are case-sensitive — 'DHL_API_KEY' and 'dhl_api_key' are different secrets. Click the secret name to verify it, then check the Edge Function code for the exact string used in Deno.env.get(). Update one or the other to match exactly.

Best practices

  • Always use DHL's sandbox environment (api-mock.dhl.com) during development — switching to production requires only changing the base URL and Cloud Secrets values, not the Edge Function logic.
  • Store origin address details (your warehouse country, postal code, city) as additional Cloud Secrets rather than hardcoding them in the Edge Function, so they're easy to update when your fulfillment location changes.
  • Log the DHL Message-Reference UUID from each API response to your Supabase database — DHL support uses this value to trace specific API calls when you report issues.
  • Validate package weight and dimension limits before calling the DHL Rate API — DHL Express has a 70kg per-piece limit and a 120cm length limit. Add client-side validation to prevent unnecessary API calls for packages that exceed DHL's thresholds.
  • Cache DHL rate responses in Supabase for identical shipment parameters with a short TTL (e.g., 10 minutes) to reduce API call volume during high-traffic checkout sessions.
  • For international shipments, set isCustomsDeclarable to true and include a valid commodityCode (HS code) in the shipment request — missing customs data causes DHL label creation to fail at the carrier level.
  • Test your production DHL credentials with a small, low-value shipment before routing live customer orders through the API, to confirm your account is provisioned for the origin-destination lanes you need.

Alternatives

Frequently asked questions

Do I need a DHL business account to use the DHL API?

Yes. The DHL Express API requires both an API key from the Developer Portal and a DHL business account number. The developer sandbox provides a test account number for building and testing, but you need a live DHL Express business account to process real shipments. Contact DHL sales at dhl.com to open an account — approval typically takes 2-3 business days.

Why must DHL API calls go through an Edge Function instead of directly from my React app?

DHL's API uses Basic Auth with your account number and API key as credentials. Calling the DHL API directly from React would expose these credentials in your browser's network tab, visible to any user with developer tools open. The Edge Function keeps credentials server-side in Cloud Secrets, where they are encrypted and inaccessible to the browser. This is Lovable's standard security pattern for any authenticated external API.

Can I use the DHL API to ship domestically as well as internationally?

DHL Express focuses on international express shipping and also offers domestic express services in many countries. However, if your primary need is domestic US shipping (ground, standard, and next-day), UPS or FedEx typically offer better domestic rates and service options. DHL is most cost-effective for international cross-border shipments where its global network and customs expertise provide the most value.

How do I switch from DHL sandbox to production?

Update two things: change the DHL_BASE_URL in your Edge Function from 'https://api-mock.dhl.com/mydhlapi' to 'https://api.dhl.com/mydhlapi', and update your Cloud Secrets to replace sandbox credentials with your live DHL API key and production account number. No other code changes are required. Redeploy the Edge Function via Lovable chat after making the URL change.

Does the DHL API support generating printable shipping labels?

Yes. The DHL Shipment API (action=shipment in this integration) returns a base64-encoded PDF label that you can decode and display as a download link in your React component. The label includes the DHL barcode, tracking number, routing information, and destination details in DHL's standard label format accepted at all DHL drop-off locations and pickup services.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.