Learn how to seamlessly connect Lovable with AfterShip using our step-by-step guide. Streamline shipment tracking and boost order management efficiency.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Create a new TypeScript file in your Lovable project. For example, create a file named afterShipIntegration.ts inside a folder called src/integrations (create the folder if it does not exist). This file will house all the code needed to connect with AfterShip’s API.
Add the following code inside afterShipIntegration.ts:
const AFTERSHIPAPIKEY = 'YOURAFTERSHIPAPI_KEY'; // Replace with your actual API key
const AFTERSHIPBASEURL = 'https://api.aftership.com/v4';
export interface TrackingInfo {
tracking_number: string;
slug: string;
}
export class AfterShipService {
// A method to track a shipment using its tracking number and slug
public async trackShipment(tracking: TrackingInfo): Promise {
const url = ${AFTERSHIP_BASE_URL}/trackings/${tracking.slug}/${tracking.tracking_number};
const response = await fetch(url, {
method: 'GET',
headers: {
'aftership-api-key': AFTERSHIPAPIKEY,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Failed to retrieve tracking information.');
}
const data = await response.json();
return data;
}
// A method to create a new tracking entry
public async createTracking(tracking: TrackingInfo): Promise {
const url = ${AFTERSHIP_BASE_URL}/trackings;
const body = {
tracking: tracking
};
const response = await fetch(url, {
method: 'POST',
headers: {
'aftership-api-key': AFTERSHIPAPIKEY,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
throw new Error('Failed to create tracking entry.');
}
const data = await response.json();
return data;
}
}
This file defines an API key and base URL for AfterShip as well as two methods: one to get tracking details and another to create a tracking entry.
Since Lovable does not provide a terminal to install Node.js dependencies, ensure your project supports the fetch API. Most modern browsers and environments have a built‐in fetch function. If your project runs in an environment that does not support fetch, you might need to include a polyfill. To do that, add the following script tag to your main HTML file:
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/3.0.0/fetch.min.js"></script>
If Lovable already supports fetch natively, you can skip this step.
Open the file in your Lovable project where you want to use the AfterShip functionality. For example, if you have a main file like app.ts or main.ts, import the AfterShip service and call its methods when needed.
Add the following snippet to your main file where you want to trigger tracking calls:
import { AfterShipService, TrackingInfo } from './integrations/afterShipIntegration';
const afterShip = new AfterShipService();
// Example function to get tracking info
async function getTrackingDetails() {
const trackingData: TrackingInfo = {
trackingnumber: 'TRACKINGNUMBER_HERE', // Replace with actual tracking number
slug: 'COURIERSLUGHERE' // Replace with the courier's slug, e.g., 'ups', 'fedex'
};
try {
const result = await afterShip.trackShipment(trackingData);
console.log('Tracking Details:', result);
// Process the returned tracking details as needed
} catch (error) {
console.error('Error fetching tracking details:', error);
}
}
// Example function to create a tracking entry
async function createNewTracking() {
const trackingData: TrackingInfo = {
trackingnumber: 'TRACKINGNUMBER_HERE',
slug: 'COURIERSLUGHERE'
};
try {
const result = await afterShip.createTracking(trackingData);
console.log('Created Tracking:', result);
// Process the API response as needed
} catch (error) {
console.error('Error creating tracking entry:', error);
}
}
// Call the functions as per your application logic
getTrackingDetails();
// createNewTracking(); // Uncomment to use the create tracking functionality
This code imports your AfterShip integration, creates an instance, and shows how to use its methods to get tracking details and create tracking entries. Replace the placeholder strings with your actual data.
Since Lovable might not have a dedicated terminal or environment variable configuration setup, securely embed your AfterShip API key directly in afterShipIntegration.ts if needed. However, be cautious with sensitive information. If your project supports runtime environment variables or a configuration file, consider moving the API key into a separate configuration file and importing it.
For example, create a file config.ts inside src with the following content:
export const config = {
AFTERSHIPAPIKEY: 'YOURAFTERSHIPAPI_KEY' // Replace with your actual API key
};
Then modify afterShipIntegration.ts to import and use the API key from config.ts:
import { config } from '../config';
const AFTERSHIPAPIKEY = config.AFTERSHIPAPIKEY;
const AFTERSHIPBASEURL = 'https://api.aftership.com/v4';
// ... rest of the code remains the same
This way you separate configuration from code even if you cannot use a terminal to set environment variables.
To verify the integration, trigger the functions from your Lovable project’s UI or call them in your main execution flow. Open your browser’s console or your application's log output to see the results of the API calls. Adjust the tracking data and error handling as needed based on the testing results.
By following these structured steps, you integrate AfterShip with your Lovable project using TypeScript. Each step includes where to place the code, what each file represents, and how to make calls to AfterShip’s API.
This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.