Introduction
As of late, I have gone through the Aarogy Setu public API which is provided by the Indian government to check the availability of the vaccine slots by Pincode number or by districts in a particular state. By checking this API, I was thinking that many people use MS Team in a number of organizations, and they are looking for a vaccination slot every day by logging in into the Cowin website. So, I started to develop code using SPFx for Ms team Tab and user can easily check vaccination slot available or on the area which is searched by the user.
Today we are accessing Setu API from SPFx solution using SPFx inbuilt library to access any external APIs and get records based on the selected Pincode or district records. Records will be displayed in a proper manner using Fluent UI and Material UI controls.
Once the solution is developed we will make some configurations to make accessing this solution available from Microsoft Teams APP.
I am not including the process to create a new SPFx solution. There are plenty of articles and blogs available to get started SPFx solution.
Click here to read about API Setu.
We have implemented the below API in this sample code.
Step 1
I am assuming that you have created your SPFx Solution and you have solution structure as below.
I am using the below name for solution artifacts.
I am using the below name for solution artifacts.
- Solution Name: CheckVaccineSlots
- WebPart Name: VaccineSlotsWebpart
- Primary Component: VaccineSlots

Step 2
Install the below required packages to solution.
- //use for display records in grid from Material UI
- npm i --save @devexpress/dx-react-core @devexpress/dx-react-grid
- //use for display records in grid from Material UI
- npm install @material-ui/core
- //use for display records in grid from Material UI
- npm i @material-ui/icons/ChevronLeft
- //use to use button, Textfield and combobox control for selection
- npm i @fluentui/react
- // Use to add datetime control
- npm i @pnp/spfx-controls-react
- // use for date manipulation and formattion
- npm i moment
Step 3
Create a service file to add generic methods to get data from APIs.
Add a new folder under src folder name "Service" and add new file "ServiceProvider.ts"
Add a new folder under src folder name "Service" and add new file "ServiceProvider.ts"

Add the below code to SeviceProvider.ts file.
- import { HttpClient, IHttpClientOptions } from '@microsoft/sp-http';
- import { WebPartContext } from '@microsoft/sp-webpart-base';
- export class ServiceProvider {
- private ctx: WebPartContext;
- /**
- * To get context from web part to use inbuilt library from SPFx
- * @param context WebpartContext
- */
- public constructor(context: WebPartContext) {
- this.ctx = context;
- }
- /**
- * Inilize client Headers which we need to use while sending request to API
- */
- private httpClientHeaders: IHttpClientOptions = {
- headers: new Headers({
- "accept": "application/json"
- }),
- method: "GET",
- mode: "cors"
- };
- /**
- *
- * @returns get All districts for selected state ID.
- * Here I put 11 for Gujarat only. You can change it according to you.
- */
- public async getAllDistricts() {
- var response = await this.ctx.httpClient.
- get("https://cdn-api.co-vin.in/api/v2/admin/location/districts/11",
- HttpClient.configurations.v1, this.httpClientHeaders);
- var responseJson: any = await response.json();
- return responseJson;
- }
- /**
- * Get available vaccination slots by selected district Id and selected date
- * @param districtId
- * @param selectedDate
- * @returns
- */
- public async getVaccinationSlots(districtId, selectedDate) {
- var endPoint = `https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict?district_id=${districtId}&date=${selectedDate}`;
- var response = await this.ctx.httpClient
- .get(endPoint,
- HttpClient.configurations.v1, this.httpClientHeaders);
- var responseJson: any = await response.json();
- return responseJson;
- }
- /**
- * get available slots by selected pincode and selected date
- * @param pincode
- * @param selectedDate
- * @returns
- */
- public async getVaccinationSlotsByPincode(pincode, selectedDate) {
- var endPoint = `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=${pincode}&date=${selectedDate}`;
- var response = await this.ctx.httpClient.get(endPoint, HttpClient.configurations.v1,
- this.httpClientHeaders);
- var responseJson: any = await response.json();
- return responseJson;
- }
- }
Step 4
Create a new file under the component with the name IVaccineSlotsState.ts to manage the state in React Application.
Add the below code in IVaccineSlotsState.ts

Add the below code in IVaccineSlotsState.ts
- import { IComboBoxOption } from "@fluentui/react";
- export interface IVaccineSlotsState{
- isLoading:boolean;
- SelectedDate:Date;
- districtsOptions:IComboBoxOption[];
- availableCenters:IComboBoxOption[];
- centers:any[];
- SelectedDistrictID: string | number;
- isSearchByPinCode:boolean;
- isSearchByDistricts:boolean;
- pincode:number;
- selectedCenterId:string|number;
- slots:any[];
- }
Step 5
Make the below changes in IVaccineSlotsProps.ts file.
- import { WebPartContext } from '@microsoft/sp-webpart-base';
- export interface IVaccineSlotsProps {
- description: string;
- context:WebPartContext;
- }
Step 6
Make the below changes in Render() to webpart.ts file to assign context which will be used in the later portion of the development.

- const element: React.ReactElement<IVaccineSlotsProps> = React.createElement(
- VaccineSlots,
- {
- description: this.properties.description,
- context:this.context
- }
- );
Step 7
Open VaccineSlots.ts file and make the below changes in an existing file.
Import the below packages on top of the file.

Import the below packages on top of the file.
- import * as React from 'react';
- import styles from './VaccineSlots.module.scss';
- import { IVaccineSlotsProps } from './IVaccineSlotsProps';
- import { IVaccineSlotsState } from "./IVaccineSlotsState";
- import { ServiceProvider } from "../../../Service/ServiceProvide"
- import { Toggle } from '@fluentui/react/lib/Toggle';
- import { TextField } from '@fluentui/react/lib/TextField';
- import {
- ComboBox,
- Stack,
- IComboBoxOption,
- IStackTokens,
- IComboBoxStyles,
- StackItem,
- } from '@fluentui/react';
- import { PrimaryButton } from '@fluentui/react/lib/Button';
- import { DateTimePicker, DateConvention, TimeConvention } from '@pnp/spfx-controls-react/lib/DateTimePicker';
- import * as moment from 'moment';
- require('moment');
- import Paper from '@material-ui/core/Paper';
- import { Grid, Table, TableHeaderRow } from '@devexpress/dx-react-grid-material-ui';
- const comboBoxStyles: Partial<IComboBoxStyles> = { root: { maxWidth: 300 } };
- const stackTokens: Partial<IStackTokens> = { childrenGap: 20 };
- /**
- * Columns is used to display column in material UI grid.
- */
- const columns = [
- { name: "name", title: "Name" },
- { name: "Address", title: "Address" },
- { name: "centerId", title: "CenterID" },
- { name: "fee_type", title: "Fee Type" },
- { name: 'Capacity', title: 'Capacity' },
- { name: 'limit', title: 'Age Limit' },
- { name: 'vaccine', title: 'Type' },
- ];
Add Constructor in React Component,






HappyCoding
Join the conversation! Your thoughts help the community grow.