I have some proprieties and i dont’k know how to integrate them with the API. I can for standart proprieties but how do i put the state for exemple ? Or the proprieties i created. Thank you for your help (PS i’m using Python)
Hello @NMusicki
Contacts API in Python that includes custom properties such as state, you can use a web framework like Flask or Django. Here’s an example using Flask:
First, install Flask by running pip install flask.
Then, create a new file called app.py and include the following code:
from flask import Flask, jsonify, request
app = Flask(__name__)
contacts = [
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "555-555-5555",
"state": "CA",
"custom_property": "some value"
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane.smith@example.com",
"phone": "555-555-5556",
"state": "NY",
"custom_property": "another value"
}
]
@app.route('/contacts', methods=['GET'])
def get_contacts():
return jsonify(contacts)
@app.route('/contacts', methods=['POST'])
def add_contact():
new_contact = {
"id": len(contacts) + 1,
"name": request.json['name'],
"email": request.json['email'],
"phone": request.json['phone'],
"state": request.json['state'],
"custom_property": request.json['custom_property']
}
contacts.append(new_contact)
return jsonify(new_contact)
@app.route('/contacts/<int:contact_id>', methods=['PUT'])
def update_contact(contact_id):
for contact in contacts:
if contact['id'] == contact_id:
contact['name'] = request.json.get('name', contact['name'])
contact['email'] = request.json.get('email', contact['email'])
contact['phone'] = request.json.get('phone', contact['phone'])
contact['state'] = request.json.get('state', contact['state'])
contact['custom_property'] = request.json.get('custom_property', contact['custom_property'])
return jsonify(contact)
return jsonify({'error': 'Contact not found'}), 404
@app.route('/contacts/<int:contact_id>', methods=['DELETE'])
def delete_contact(contact_id):
for contact in contacts:
if contact['id'] == contact_id:
contacts.remove(contact)
return jsonify({'message': 'Contact deleted'})
return jsonify({'error': 'Contact not found'}), 404
if __name__ == '__main__':
app.run(debug=True)
list of contacts with standard properties (id, name, email, and phone) as well as custom properties (state and custom_property). The GET route returns a list of all contacts, while the POST route adds a new contact to the list. The PUT route updates an existing contact, and the DELETE route removes a contact from the list.
To test the API, you can use a tool like Postman or cURL to send HTTP requests to the API endpoints. For example, to add a new contact, send a POST request to http://localhost:5000/contacts with a JSON payload like this:
{
"name": "Bob Johnson",
"email": "bob.johnson@example.com",
"phone": "555-555-5557",
"state": "TX",
"custom_property": "some value"
}
This will add a new contact to the list with the specified properties, including the custom properties.
Thank you a lot for your answer and your reactivity. I can affect the API calls with request library. But i don’t understand where can i find the custom propriety name i should put in the dictionnary. Each time it told me : this propriety does not exist, so I would like to see if there is a place where I can find the properties to place in the dictionary. For example, the marketing contract statut, where do i find the right word fot the API call ? Hope my request is more clear.
Thank you