Learn to integrate Lovable with DHL API effortlessly. Our step-by-step guide covers setup, troubleshooting, and best practices for streamlined shipping.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
dhlService.ts
.dhlService.ts
:
// Replace these credentials with your actual DHL API credentials
const DHLAPIKEY = 'YOURDHLAPI_KEY';
const DHLAPISECRET = 'YOURDHLAPI_SECRET';
const DHLBASEURL = 'https://api.dhl.com';
// Example function to track a shipment via DHL API using the tracking number
export async function trackShipment(trackingNumber: string): Promise {
// Construct the full URL with the tracking number
const url = ${DHL_BASE_URL}/track/shipments?trackingNumber=${trackingNumber};
// In place of installing axios via terminal (since Lovable doesn't offer a terminal),
// we use the native fetch API.
// If you must use axios, manually include its script tag in your HTML and import it below.
const headers = {
'DHL-API-Key': DHLAPIKEY,
'Content-Type': 'application/json'
};
try {
const response = await fetch(url, { method: 'GET', headers });
if (!response.ok) {
throw new Error(`Error fetching tracking info: ${response.statusText}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error in trackShipment:', error);
throw error;
}
}
app.ts
) where you want to invoke the DHL API.trackShipment
function in that file and use it as needed. Insert the following code in your app.ts
:
import { trackShipment } from './dhlService';
// This function can be triggered when the user needs shipment tracking info.
// For example, it could be attached to a button click event.
async function showShipmentTracking(trackingNumber: string) {
try {
const trackingData = await trackShipment(trackingNumber);
// Process or display the trackingData as required for your app
console.log('DHL Tracking Data:', trackingData);
} catch (error) {
console.error('Failed to retrieve tracking information:', error);
}
}
// Example usage: Call the function with a dummy tracking number
// Remove or modify this code according to your application flow.
showShipmentTracking('YOURTRACKINGNUMBER');
fetch
as shown in the snippet.<head>
section:
<script src="PATHTOYOUR_LIB/axios.min.js"></script>
YOURDHLAPIKEY
, YOURDHLAPISECRET
, and YOURTRACKINGNUMBER
) with your actual DHL API credentials and test tracking number.dhlService.ts
and app.ts
files.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.