Skip to content

Python Stretch API SDK Library

The Stretch API SDK Library is a powerful Python library designed to simplify the integration and interaction with the Stretch API. It provides developers with a convenient way to access and utilize the various features and functionalities offered by the Stretch API.

Features

  • Asynchronous and Synchronous Functionality: The Stretch API SDK Library offers support for both asynchronous and synchronous operations. Developers can choose to perform API requests synchronously or leverage the library's asynchronous capabilities to make non-blocking, concurrent requests.
  • OAuth2 Authentication: The library seamlessly supports OAuth2 authentication using JSON Web Tokens (JWT). This allows developers to securely authenticate and authorize their applications to access the Stretch API.
  • Coach Delivery at Home: The Stretch API SDK Library provides extensive functionality for coach delivery at home. Developers can easily leverage the library to manage and coordinate the delivery of coaches to customers' homes, streamlining the entire process, payments.

Getting Started

To get started with the Stretch API SDK Library, follow these simple steps: 1. Installation: Install the library by running the following command:

pip install stretch-api
2. Importing the Library: Import the library in your Python project:
from stretch import Stretch
3. Authentication: Authenticate your application using the OAuth2 authentication method:
stretch = Stretch(client_id)
if stretch.auth.login(username, password):
    print("Login success")

Utilizing the Functionality:

Start utilizing the various features and functionalities provided by the Stretch API SDK Library, such as coach search and book:

Get info from auth user

user = stretch.auth.get_user()
print("My info:", user)

Search coaches:

# Get all available coaches in you location
coaches = stretch.search.post()
if not coaches:
    raise Exception("Empty coach list")
print("List of coaches:", coaches)

Get services from first coach

# Get profile info from first coach
services = stretch.coach.get_services(coaches[0]['id'])
if not services:
    raise Exception("Empty services list")
print(services)

Get time for booking

Take first service from this coach and get time for booking

availabilities = stretch.service.get_availability(services[0]['id'])
if not availabilities:
    raise Exception("Coach not have available time for booking")

Create booking

Create booking on first available time (available payments method you can get from stretch.payment.get_methods())

booking = stretch.service.post_booking(services[0]['id'],{
    "slots": availabilities[0],
    "method": "card"
})
if not booking:
    raise Exception("Can not booking")
print("Booking info", booking)

Checkout

Booking created and available for online payment

# Create checkout for booking
payment = stretch.payment.post_checkout(booking['payment_id'])

print("Client secret:", payment['client_secret'])
Payments method depends on provider

Online payment

For online payment we use Stripe For implementation payment proccess on your site you must add next code your_stripe_publishable_key - you can take from Admin dashboard section payments methods

<!-- Include Stripe.js library -->
<script src="https://js.stripe.com/v3/"></script>

<!-- Create a payment form -->
<form id="payment-form">
  <div id="card-element">
    <!-- Stripe will inject the card input fields here -->
  </div>
  <button type="submit">Pay Now</button>
</form>

<script>
  // Set your Stripe publishable key
  var stripe = Stripe('your_stripe_publishable_key', clientSecret=<clientSecret>);

  // Create a card Element
  var elements = stripe.elements();
  var cardElement = elements.create('card');

  // Mount the card Element to the card-element div
  cardElement.mount('#card-element');

  // Handle form submission
  var form = document.getElementById('payment-form');

  // Handle the payment on your server
  function handlePayment(paymentMethodId) {
    // Send the paymentMethodId to your server via an API request
    fetch('/process_payment', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ paymentMethodId: paymentMethodId })
    })
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      if (data.success) {
        // Payment succeeded
        console.log('Payment successful!');
      } else {
        // Payment failed
        console.log('Payment failed.');
      }
    });
  }
</script>

Payment confirmation

Get payment confirmation from platform

payment = stretch.payment.get_payment(payment_id)
print('Check payment status:', payment)
After success payment status must by: received

Documentation

For detailed information on the Stretch API SDK Library and its functionalities, refer to the official repository available at github repository.

Support

If you require any assistance or have any questions, please reach out to our support team at [email protected].

Start integrating the Stretch API SDK Library into your Python applications today and unlock the power of seamless coach delivery at home.