Access RedBus api to book tickets
The **RedBus API** allows third-party applications to integrate with their platform for bus ticket bookings, seat availability checks, and other related services. However, access to the RedBus API is usually restricted to authorized partners. You'll need to contact RedBus and sign up for their **API Partner Program** to obtain access.
Here’s how you can work with the **RedBus API** for booking seats, assuming you have the necessary credentials (API key, secret, etc.):
### Steps to Use RedBus API for Seat Booking:
1. **Obtain API Access**:
- Contact RedBus for **API Access** at their [RedBus Partner Program](https://www.redbus.in/partner-with-us).
- After approval, you will receive an API Key, Client ID, and Client Secret.
2. **Endpoints Overview**:
The RedBus API offers various endpoints. Here are the main ones relevant to seat booking:
- **Search for Buses**: Search for buses between two locations.
- **Check Seat Availability**: Get the layout and availability of seats.
- **Block Seats**: Temporarily block seats before booking.
- **Confirm Booking**: Complete the seat booking after blocking the seats.
### Sample Python Code for Using RedBus API (Assuming Access)
Here’s an outline of how you might interact with the RedBus API:
#### 1. **Search for Buses** (Search endpoint example)
This API call allows you to find available buses between two locations.
```python
import requests
# Replace with your actual API key
api_key = 'your_api_key_here'
# Endpoint for searching buses
url = 'https://api.redbus.in/v1/search'
# Parameters for the search (example)
params = {
'source': 'Mumbai', # Source city
'destination': 'Pune', # Destination city
'date_of_journey': '2024-09-20' # Date of journey in yyyy-mm-dd format
}
# Headers
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
# Send the GET request
response = requests.get(url, headers=headers, params=params)
# Check if the request was successful
if response.status_code == 200:
buses = response.json()
for bus in buses['results']:
print(f"Bus Operator: {bus['operator_name']}, Bus Type: {bus['bus_type']}")
else:
print(f"Error: {response.status_code}")
```
#### 2. **Check Seat Availability**
Once you have bus details from the search API, you can check the seat layout and availability using a seat availability endpoint.
```python
# URL for checking seat availability
seat_availability_url = f'https://api.redbus.in/v1/seats/{bus_id}'
# bus_id obtained from the search result
bus_id = 'some_bus_id_here'
# Send request for seat availability
response = requests.get(seat_availability_url, headers=headers)
if response.status_code == 200:
seat_data = response.json()
print(f"Available Seats: {seat_data['availableSeats']}")
else:
print(f"Error: {response.status_code}")
```
#### 3. **Block Seats** (Before Confirming Booking)
Once seats are chosen, they must be blocked temporarily before the booking is confirmed.
```python
# URL for blocking seats
block_url = 'https://api.redbus.in/v1/blockSeats'
# Example payload for blocking seats
payload = {
'bus_id': bus_id, # Bus ID obtained from the seat availability response
'seats': ['A1', 'A2'], # Seat numbers to block
'contact_info': {
'name': 'John Doe',
'email': 'john@example.com',
'phone': '9876543210'
},
'payment_info': {
'payment_method': 'credit_card'
}
}
# Send the POST request to block seats
response = requests.post(block_url, headers=headers, json=payload)
if response.status_code == 200:
block_data = response.json()
print(f"Seats Blocked: {block_data['block_id']}")
else:
print(f"Error: {response.status_code}")
```
#### 4. **Confirm Booking**
After successfully blocking seats, the next step is to confirm the booking.
```python
# URL for confirming booking
confirm_booking_url = 'https://api.redbus.in/v1/confirmBooking'
# Payload for confirming the booking
payload = {
'block_id': block_data['block_id'], # Block ID from the block seats API response
'payment_status': 'Success' # Payment confirmation status
}
# Send POST request to confirm booking
response = requests.post(confirm_booking_url, headers=headers, json=payload)
if response.status_code == 200:
booking_data = response.json()
print(f"Booking Successful: {booking_data['ticket_number']}")
else:
print(f"Error: {response.status_code}")
```
### Key Considerations:
- **Authentication**: You’ll need to send API requests with your API Key and token for authentication.
- **Error Handling**: Handle errors such as invalid bus IDs, unavailable seats, or failed payments.
- **Payment Integration**: For real-world implementation, you’ll need to integrate a payment gateway to confirm the booking.
No Comments have been Posted.