Learn to integrate Bolt.new AI with Freightos for improved shipping logistics. Follow our step-by-step guide for streamlined, efficient 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.
freightos.ts
in the root directory.freightos.ts
:
export async function getFreightRates(origin: string, destination: string, cargoDetails: any): Promise {
// Retrieve your Freightos API key from environment variables.
// Make sure to add FREIGHTOSAPIKEY to your project's secrets settings.
const apiKey = process.env.FREIGHTOSAPIKEY;
if (!apiKey) {
throw new Error("Freightos API key is not configured.");
}
// Replace the URL below with the actual Freightos API endpoint if different.
const apiUrl = "https://api.freightos.com/v1/rates";
// Build the request payload using provided parameters.
const payload = {
origin,
destination,
cargo: cargoDetails
};
// Perform a POST request to Freightos API.
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": Bearer ${apiKey}
},
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error("Failed to fetch freight rates: " + errorText);
}
return await response.json();
}
FREIGHTOSAPIKEY
and set its value to your Freightos API key.process.env.FREIGHTOSAPIKEY
.
index.ts
or similar).getFreightRates
function from the newly created freightos.ts
file.
import { getFreightRates } from "./freightos";
// Example function that could be triggered by a user action or API request.
async function handleGetFreightRates() {
try {
// Example parameters. Replace with actual data as required.
const origin = "NYC";
const destination = "LAX";
const cargoDetails = {
weight: 1000, // weight in lbs or kg based on your requirements
dimensions: { length: 10, width: 5, height: 4 }
};
const rates = await getFreightRates(origin, destination, cargoDetails);
console.log("Freight rates:", rates);
// Handle the rates data (e.g., update UI, forward to another service, etc.)
} catch (error) {
console.error("Error retrieving freight rates:", error);
}
}
// For demonstration, call the function immediately.
// In your application, integrate this call with your event logic.
handleGetFreightRates();
fetch
function. If you encounter issues with fetch
, consider adding a polyfill.<head>
section:
<script src="https://cdnjs.cloudflare.com/ajax/libs/whatwg-fetch/3.6.2/fetch.umd.js"></script>
handleGetFreightRates
function in your application.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.