Discover how to integrate Bolt.new AI with the UPS API using our step-by-step guide. Streamline shipping operations and boost automation for your business.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
upsConfig.ts
in your project’s root directory.upsConfig.ts
:
export const upsConfig = {
accessKey: "YOURUPSACCESS_KEY",
userId: "YOURUPSUSER_ID",
password: "YOURUPSPASSWORD",
accountNumber: "YOURUPSACCOUNT_NUMBER",
// Set API endpoint; use testing endpoint if applicable
endpoint: "https://onlinetools.ups.com/rest/Rate"
};
upsService.ts
in your project’s root directory.fetch
method in TypeScript to avoid additional dependencies.upsService.ts
:
import { upsConfig } from "./upsConfig";
interface RateRequestData {
shipper: any;
shipTo: any;
service: any;
package: any;
// Add additional types as needed
}
export async function getShippingRates(data: RateRequestData): Promise<any> {
// Construct the request payload with credentials and shipment details
const requestBody = {
UPSSecurity: {
UsernameToken: {
Username: upsConfig.userId,
Password: upsConfig.password
},
ServiceAccessToken: {
AccessLicenseNumber: upsConfig.accessKey
}
},
RateRequest: {
Request: {
RequestOption: "Rate"
},
Shipment: {
Shipper: data.shipper,
ShipTo: data.shipTo,
Service: data.service,
Package: data.package
}
}
};
try {
const response = await fetch(upsConfig.endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(UPS API error: ${response.status} ${response.statusText});
}
return await response.json();
} catch (error) {
console.error("Error calling UPS API:", error);
throw error;
}
}
index.ts
), import the function you just created from upsService.ts
.getShippingRates
function when needed (for instance, in response to a user event or a scheduled task).
import { getShippingRates } from "./upsService";
// Example shipment data; adjust according to your requirements
const shipmentData = {
shipper: {
Name: "Sender Name",
ShipperNumber: "YOURUPSACCOUNT_NUMBER",
Address: {
AddressLine: "123 Origin St.",
City: "OriginCity",
StateProvinceCode: "OC",
PostalCode: "12345",
CountryCode: "US"
}
},
shipTo: {
Name: "Receiver Name",
Address: {
AddressLine: "789 Destination Ave.",
City: "DestCity",
StateProvinceCode: "DC",
PostalCode: "67890",
CountryCode: "US"
}
},
service: {
Code: "03", // UPS Ground service code or another code
Description: "UPS Ground"
},
package: {
PackagingType: {
Code: "02",
Description: "Package"
},
Dimensions: {
UnitOfMeasurement: {
Code: "IN"
},
Length: "10",
Width: "7",
Height: "5"
},
PackageWeight: {
UnitOfMeasurement: {
Code: "LBS"
},
Weight: "5"
}
}
};
// Function to trigger UPS rate calculation
async function fetchShippingRates() {
try {
const rateResponse = await getShippingRates(shipmentData);
console.log("UPS Shipping Rates:", rateResponse);
// Further processing of rateResponse as needed
} catch (error) {
console.error("Error fetching UPS shipping rates:", error);
}
}
// Call the function on application start or as part of your workflow
fetchShippingRates();
upsConfig.ts
, upsService.ts
, and your main file), save all changes.fetch
.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.