Learn how to integrate Lovable with Shippo for streamlined shipping management. Our step-by-step guide makes setup quick and efficient.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json
file in your Lovable project.
"dependencies": {
"shippo": "^1.0.0",
// ... other dependencies
}
services
under your src
directory if it does not already exist.src/services
folder, create a file named shippoService.ts
. This file will handle all interactions with the Shippo API.
shippoService.ts
file and insert the following TypeScript code snippet:
import * as Shippo from 'shippo';
// Initialize the Shippo client using your API key.
// Make sure to set process.env.SHIPPOAPIKEY in your environment configuration.
const shippo = Shippo(process.env.SHIPPOAPIKEY);
/**
- Creates a shipment using Shippo API.
- @param fromAddress - The sender's address object.
- @param toAddress - The recipient's address object.
- @param parcels - An array of parcel objects.
- @returns The created shipment details.
*/
export async function createShipment(fromAddress: any, toAddress: any, parcels: any[]): Promise {
try {
const shipment = await shippo.shipment.create({
address_from: fromAddress,
address_to: toAddress,
parcels: parcels,
async: false // Set to false for synchronous processing
});
return shipment;
} catch (error: any) {
throw new Error(Error creating shipment: ${error.message});
}
}
env.ts
file, insert the following (replace YOURSHIPPOAPI_KEY with your actual API key):
export const SHIPPOAPIKEY = 'YOURSHIPPOAPI_KEY';
process.env.SHIPPOAPIKEY
in your shippoService.ts
file correctly references the API key. If needed, import and assign it appropriately.
createShipment
function from shippoService.ts
into the relevant file. For example, in orderController.ts
:
import { createShipment } from './services/shippoService';
async function processOrder(order: any) {
// Define from (sender) and to (recipient) addresses according to your order data
const fromAddress = {
name: 'Sender Name',
street1: '123 Shippo St.',
city: 'San Francisco',
state: 'CA',
zip: '94107',
country: 'US'
};
const toAddress = {
name: order.customerName,
street1: order.shippingAddress.street,
city: order.shippingAddress.city,
state: order.shippingAddress.state,
zip: order.shippingAddress.zip,
country: order.shippingAddress.country
};
const parcels = [{
length: '10',
width: '5',
height: '8',
distance_unit: 'in',
weight: '2',
mass_unit: 'lb'
}];
try {
const shipment = await createShipment(fromAddress, toAddress, parcels);
// You can now use the shipment details (e.g., display shipping rates, tracking info, etc.)
console.log('Shipment created:', shipment);
} catch (error) {
console.error('Failed to create shipment:', error);
}
}
processOrder
when a new order is placed to verify the Shippo integration works as expected.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.