Petstore APIs were designed and developed by John Smith as a hobby project. He created them on his weekends. One day he decided to make them open source by creating a public repository on Github. Then he used them to start a small business as they grew. This document covers the integration process required to use these APIs' "pet" section. You can add, remove, update, and retrieve pet data using these APIs.
Take a look at this petstore.swagger.io to start integrating these APIs. It gives you a lot of information at a glance. You can even test every API by clicking on "Try it out" button.
Endpoint | /pet/ |
Sample url | https://petstore.swagger.io/v2/pet/ |
Name | Description | Required | Type |
---|---|---|---|
body | Pet object that needs to be added to the store | true | object |
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
Status code | Response |
---|---|
200 | { "id": 9223372036854043000, "category": { "id": 0, "name": "string" }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 0, "name": "string" } ], "status": available"} |
405 | Invalid Input |
Endpoint | /pet/{petId} |
Sample url | https://petstore.swagger.io/v2/pet/3341 |
Name | Description | Required | Type |
---|---|---|---|
petId | ID of pet to return | true | string |
Status code | Response |
---|---|
200 | { "id": 0, "category": { "id": 0, "name": "string" }, "name": "doggie", "photoUrls": "string" ], "tags": [ { "id": 0, "name": "string" } ], "status": "available"} |
400 | Invalid ID supplied |
400 | Pet not found |
Endpoint | /pet/{petId} |
Sample url | https://petstore.swagger.io/v2/pet/3341 |
Name | Description | Required | Type | Location |
---|---|---|---|---|
api_key | The key you need to authorize yourself | false | string | Header |
petId | Pet id to delete | true | string | Querystring |
For this sample, you can use the api key special-key
to test the authorization filters.
Status code | Response |
---|---|
200 | { "code": 200,"type": "unknown","message": "{petId}}"} |
400 | Invalid ID supplied |
400 | Pet not found |
Using this piece of code in python, you can get a list of all pets base on their status. The status can be one of the followings:
- Available
- Pending
- Sold
This code returns a list of 10 pets with unique names and available
status
To run this code snippet, you need to install requests
module by running the following command.
pip install requests
Once you finish the installation process, save the code in a file named app.py
and run it by running the following command.
python app.py
import requests
response = requests.get("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
pets = response.json()
uniqueNames = []
i = 0
while (len(uniqueNames)<=9):
if(pets[i]['name'] not in uniqueNames):
uniqueNames.append(pets[i]['name'])
print(pets[i]['id'] , pets[i]['name'])
i = i + 1