Integrate Bolt.new AI with AfterShip and boost your shipping efficiency. Follow our step-by-step guide to streamline tracking and enhance your operations.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Since Bolt.new AI doesn’t support a terminal, you need to add any dependency directly in your project’s package.json file. Open your package.json and add the following within the "dependencies" section to include axios:
{
"dependencies": {
"axios": "0.27.2"
// Other dependencies if needed
}
}
In your project’s file tree, create a new file at src/integrations/aftership.ts
. This file will contain the TypeScript code needed to integrate with AfterShip. Paste the following code into that file:
import axios from "axios";
// Replace with your actual AfterShip API key or better load it from env variables
const AFTERSHIPAPIKEY = process.env.AFTERSHIPAPIKEY || "YOURAFTERSHIPAPI_KEY";
// Base URL for the AfterShip API
const AFTERSHIPBASEURL = "https://api.aftership.com/v4";
/**
- Track a shipment using AfterShip API.
- @param slug - The courier slug (e.g., "ups", "fedex").
- @param trackingNumber - The shipment tracking number.
*/
export async function trackShipment(slug: string, trackingNumber: string): Promise<any> {
try {
const url = ${AFTERSHIP_BASE_URL}/trackings/${slug}/${trackingNumber};
const response = await axios.get(url, {
headers: {
"aftership-api-key": AFTERSHIPAPIKEY,
"Content-Type": "application/json"
}
});
return response.data;
} catch (error) {
throw new Error(Error fetching tracking data: ${error});
}
}
Decide where in your Bolt.new AI project you need to use shipment tracking. For example, if your main application logic is in src/index.ts
, open that file and import the newly created function. Add the code snippet in the place where you want to perform tracking:
import { trackShipment } from "./integrations/aftership";
// Example usage: Tracking a shipment
async function demoTracking() {
const courierSlug = "ups"; // Replace with the appropriate courier slug
const trackingNum = "1Z999AA10123456784"; // Replace with a real tracking number
try {
const trackingData = await trackShipment(courierSlug, trackingNum);
console.log("Tracking Data:", trackingData);
} catch (error) {
console.error("Error tracking shipment:", error);
}
}
// Call the demoTracking function wherever it fits in your application flow.
demoTracking();
To securely store your AfterShip API key, add it to your environment variables. In Bolt.new AI, you can typically set environment variables using the project’s secrets configuration. Assign your API key to the variable name AFTERSHIPAPIKEY. This way, the code in aftership.ts
can access the key securely via process.env.AFTERSHIPAPIKEY
.
After you have added the dependency setup in package.json, created the src/integrations/aftership.ts
file, and imported the tracking function in your main file, save your changes. Use the Bolt.new AI interface’s Run button to execute your project. The console should display tracking information if the tracking number and courier slug are valid and your API key is correctly configured.
Ensure that you replace placeholder values such as "YOURAFTERSHIPAPI_KEY", courier slug, and tracking number with actual values. Also, remember to configure your environment variables in Bolt.new AI’s secrets management section for production use.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.