Hypi’s low code platform offers many features. REST APIs are one of them. REST APIs follow REST architectural style and allows interaction with RESTful web services.
Let’s see how to use the Python Requests module to interact with Hypi’s REST APIs!
Python is a language with pretty clear and easy-to-use syntax. Hence, it is very easy to interact with REST APIs from Python. It has specifically designed the ‘Requests’ library for that. The library is pretty powerful in making an HTTP request to any API.
Let’s get started with the interaction between Python and Hypi.
Four basic components are needed for data transfer.
- Endpoint: Hypi’s main endpoint https://api.hypi.app/rest processes standard REST-like requests against an instance.
- Method: GET, POST, PUT, DELETE are the main methods that are used to get, create, update and delete data respectively.
- Data: Mostly REST requests will contain raw data.
- Headers: Authorization token, hypi-domain, and content-type are the main headers needed to connect with Hypi. Content-type is always ‘application/json’.
You may check Hypi’s REST API documentation on how to formulate the API endpoint for different kinds of requests.
Python Requests library Installation
Make sure you have Python and Pip installed on your machine. If you are using Python 2 >=2.7.9 or Python 3 >=3.4, pip must be already installed. If not, check this installation guide.
Go to the command line and install the Requests library using the following command.
>>>pip install requests
Before making REST requests, import the requests library.
>>> import requests
Now you are ready to make REST requests to Hypi.
Login
First, we will log in to the Hypi account and get the authorization token.
>>> my_headers = {'hypi-domain' : 'delirious.apps.hypi.app' , 'content-type' : 'application/json'}
>>> response = requests.get('https://api.hypi.app/rest/v1/fn/query/login?username=user1&[email protected]&type=query', headers=my_headers)
>>> response.text
'{"data":{"login":{"__typename":"AccessToken","hypi":null,"sessionToken":"Auth-Token","sessionExpires":1628317911,"errorCode":null,"errorMsg":null}}}'
Here, we have set the content-type and hypi-domain under my_headers. Using the get method of requests we have sent the login request to Hypi.
Note the endpoint used.
https://api.hypi.app/rest/v1/fn/query/login?username=user1&password=user1
Username and Password are appended to the https://api.hypi.app/rest/v1/fn/query/login as query parameters. We store the response in the response variable and we can see the text of the response using response.text command. As you can see we got the session token which we can use to make subsequent CRUD requests.
Create Data
We will create a Post object with the text field. We have already declared the Post data type in the schema of the instance.
>>> my_headers = {'hypi-domain' : 'delirious.apps.hypi.app' , 'content-type' : 'application/json' , 'authorization' : 'Auth-Token'}
>>> raw_data = '{"values":{"Post":[{"text": "This is from Python"}]}}'
>>> response = requests.post('https://api.hypi.app/rest/v1/fn/mutation/upsert',data=raw_data,headers=my_headers)
>>> response.text
'{"data":{"upsert":[{"__typename":"Hypi","id":"01FA2KKZR9MYK7R3J4HTW3YJ43","impl":null,"created":"2021-07-08T08:26:48Z","updated":"2021-07-08T08:26:48Z","trashed":null,"createdBy":"01F7WRCDQYQYHZ0K3TMEBFEGBY","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null}]}}'
Data create/update requests can be sent to https://api.hypi.app/rest/v1/fn/mutation/upsert endpoint. We have added Authorization into the my_header. We have set raw_data as the body of the text. raw_data is nothing but the JSON with values variables which we commonly use with upsert.
We have used post method to create the data. As you can see in the response, an object with hypi.id 01FA2KKZR9MYK7R3J4HTW3YJ43 got created.
Update Data
Updating data is similar to creating it. We use the put method instead of post. And we pass on earlier created hypi id in the raw_data.
>>> raw_data = '{"values":{"Post":[{"hypi": {"id": "01FA2KKZR9MYK7R3J4HTW3YJ43"},"text": "This is updated text from Python"}]}}'
>>> response = requests.put('https://api.hypi.app/rest/v1/fn/mutation/upsert',data=raw_data,headers=my_headers)
>>> response.text
'{"data":{"upsert":[{"__typename":"Hypi","id":"01FA2KKZR9MYK7R3J4HTW3YJ43","impl":null,"created":null,"updated":"2021-07-08T08:30:43Z","trashed":null,"createdBy":null,"instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null}]}}'
Here we have updated the text.
Get data
We have used the get method to retrieve the data using hypi id. Note the endpoint https://api.hypi.app/rest/v1/Post/01FA2KKZR9MYK7R3J4HTW3YJ43
Here we have appended data type Post and hypi.id of the object to the main REST endpoint.
>>> response = requests.get('https://api.hypi.app/rest/v1/Post/01FA2KKZR9MYK7R3J4HTW3YJ43',headers=my_headers)
>>> response.text
'{"data":{"get":{"__typename":"Post","hypi":{"__typename":"Hypi","id":"01FA2KKZR9MYK7R3J4HTW3YJ43","impl":null,"created":"2021-07-08T08:26:48Z","updated":"2021-07-08T08:30:43Z","trashed":null,"createdBy":"01F7WRCDQYQYHZ0K3TMEBFEGBY","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"postedby":{"__typename":"Account","hypi":{"__typename":"Hypi","id":"01F7WRCDQYQYHZ0K3TMEBFEGBY","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F7WRCDQYQYHZ0K3TMEBFEGBY","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"verified":false,"enabled":true,"username":"user1","password":{"__typename":"Password","hypi":{"__typename":"Hypi","id":"01F7WRCDWQKPFXKYWCX40RJ5F7","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F2GA50NFXHMYCBDNYFJK1V7R","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"value":"[redacted]","expired":null},"owner":null,"emails":[{"__typename":"Email","hypi":{"__typename":"Hypi","id":"01F7WRCEA5NP8KSFJ1MF1G2X1P","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F2GA50NFXHMYCBDNYFJK1V7R","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"value":"[email protected]","type":null}],"phones":null,"groups":null,"roles":null,"attempts":null,"remoteLogins":null},"date":null,"text":"This is updated text from Python","description":null,"likecount":0,"rating":null,"share":null,"postuuid":null,"gppost":null,"likedby":null,"comments":null,"tagFriends":null}}}'
Find data with ArcQL
We can retrieve data by appending the arcql statement as a query parameter to the endpoint. Here arcql statement is arcql=text ~ \‘python\’ which gets the object having python written in the text fields. You may frame your own arcql statements.
>>> response = requests.get('https://api.hypi.app/rest/v1/Post?arcql=text~\'python\'',headers=my_headers)
>>> response.text
'{"data":{"get":{"__typename":"Post","hypi":{"__typename":"Hypi","id":"01FA2KKZR9MYK7R3J4HTW3YJ43","impl":null,"created":"2021-07-08T08:26:48Z","updated":"2021-07-08T08:30:43Z","trashed":null,"createdBy":"01F7WRCDQYQYHZ0K3TMEBFEGBY","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"postedby":{"__typename":"Account","hypi":{"__typename":"Hypi","id":"01F7WRCDQYQYHZ0K3TMEBFEGBY","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F7WRCDQYQYHZ0K3TMEBFEGBY","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"verified":false,"enabled":true,"username":"user1","password":{"__typename":"Password","hypi":{"__typename":"Hypi","id":"01F7WRCDWQKPFXKYWCX40RJ5F7","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F2GA50NFXHMYCBDNYFJK1V7R","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"value":"[redacted]","expired":null},"owner":null,"emails":[{"__typename":"Email","hypi":{"__typename":"Hypi","id":"01F7WRCEA5NP8KSFJ1MF1G2X1P","impl":null,"created":"2021-06-11T05:23:13Z","updated":"2021-06-11T05:23:13Z","trashed":null,"createdBy":"01F2GA50NFXHMYCBDNYFJK1V7R","instanceId":"01F52B07VZN5XBTMZEZPDS6FJF","tags":null},"value":"[email protected]","type":null}],"phones":null,"groups":null,"roles":null,"attempts":null,"remoteLogins":null},"date":null,"text":"This is updated text from Python","description":null,"likecount":0,"rating":null,"share":null,"postuuid":null,"gppost":null,"likedby":null,"comments":null,"tagFriends":null}}}'
Delete
We have deleted the created object using the delete method. The main endpoint remains the same with Post object and hypi ID. We have set clearArrayReferences to true in the query parameter in the url.
>>> response = requests.delete('https://api.hypi.app/rest/v1/Post/01FA2KKZR9MYK7R3J4HTW3YJ43?clearArrayReferences=true',headers=my_headers)
>>> response.text
'{"data":{"delete":1}}'
The object has been successfully deleted.
Conclusion:
As you have seen, it is easy to send REST requests to Hypi using the Python Request module. If you have any further queries/suggestions do let us know.