rentalcar-api-sdk
v1.0.0
Published
SDK for RentalCar Manager API integration
Readme
RentalCar Manager API Integration Guide
Environment Variables
RENTALCAR_API_KEY=your_api_key
RENTALCAR_SHARED_SECRET=your_shared_secret
RENTALCAR_API_URL=https://api.rentalcarmanager.com/v32Authentication Flow
Token Generation
- Endpoint:
${RENTALCAR_API_URL}/token - Method: POST
- Headers:
Content-Type: application/x-www-form-urlencoded - Body:
username=${RENTALCAR_API_KEY}&password=${RENTALCAR_SHARED_SECRET}&grant_type=password - Response:
{ "access_token": "token_value", "token_type": "bearer", "expires_in": 1799 } - Token validity: 30 minutes
- Endpoint:
API Methods
Find Booking
- Endpoint:
${RENTALCAR_API_URL}/api - Method: POST
- Headers:
Content-Type: application/json Authorization: Bearer ${token} - Body:
{ "method": "findbooking", "reservationno": "reservation_number", "lastname": "customer_lastname" } - Response:
{ "status": "OK", "error": "", "results": [ { "reservationref": "reference_id" } ] }
Booking Info
- Endpoint:
${RENTALCAR_API_URL}/api - Method: POST
- Headers:
Content-Type: application/json Authorization: Bearer ${token} - Body:
{ "method": "bookinginfo", "reservationref": "reference_id" } - Response: Detailed booking information
Example Implementation
// src/services/rentalcar/types.ts
export interface RentalCarConfig {
apiKey: string;
sharedSecret: string;
apiUrl: string;
}
export interface TokenResponse {
access_token: string;
token_type: string;
expires_in: number;
}
export interface BookingSearchParams {
reservationno: string;
lastname: string;
}
// src/services/rentalcar/client.ts
export class RentalCarClient {
private token: string | null = null;
private tokenExpiration: Date | null = null;
constructor(private config: RentalCarConfig) {}
private async getToken(): Promise<string> {
if (
this.token &&
this.tokenExpiration &&
new Date() < this.tokenExpiration
) {
return this.token;
}
const params = new URLSearchParams({
username: this.config.apiKey,
password: this.config.sharedSecret,
grant_type: "password",
});
const response = await fetch(`${this.config.apiUrl}/token`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: params,
});
if (!response.ok) {
throw new Error("Failed to get token");
}
const data: TokenResponse = await response.json();
this.token = data.access_token;
this.tokenExpiration = new Date(Date.now() + (data.expires_in - 60) * 1000);
return this.token;
}
async findBooking(params: BookingSearchParams) {
const token = await this.getToken();
const response = await fetch(`${this.config.apiUrl}/api`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
method: "findbooking",
...params,
}),
});
return response.json();
}
async getBookingInfo(reservationref: string) {
const token = await this.getToken();
const response = await fetch(`${this.config.apiUrl}/api`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
method: "bookinginfo",
reservationref,
}),
});
return response.json();
}
}Usage Example
// Initialize client
const client = new RentalCarClient({
apiKey: process.env.RENTALCAR_API_KEY!,
sharedSecret: process.env.RENTALCAR_SHARED_SECRET!,
apiUrl: process.env.RENTALCAR_API_URL!,
});
// Find booking
const booking = await client.findBooking({
reservationno: "12345",
lastname: "Smith",
});
// Get booking details
const details = await client.getBookingInfo(booking.results[0].reservationref);Error Handling
The API returns errors in the following format:
{
"status": "ERR",
"error": "Error message",
"results": null
}Common error codes:
- 401: Authentication failed
- 400: Invalid parameters
- 404: Booking not found
- 500: Server error
Best Practices
Token Management
- Cache the token until near expiration
- Implement automatic token refresh
- Handle token errors gracefully
Error Handling
- Implement retry logic for network errors
- Log API errors for debugging
- Provide meaningful error messages to users
Security
- Never expose API keys in client-side code
- Store sensitive data in environment variables
- Implement rate limiting if needed
