Basic Usage Examples
Learn how to use ContactList with these basic examples. These examples demonstrate common use cases and patterns.
Creating a Contact
Here's how to create a new contact using the API:
JavaScript/TypeScript
async function createContact(contactData) {
const response = await fetch('https://api.contactlist.io/v1/contacts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.CONTACTLIST_API_KEY}`
},
body: JSON.stringify({
name: contactData.name,
email: contactData.email,
phone: contactData.phone
})
});
if (!response.ok) {
throw new Error(`Failed to create contact: ${response.statusText}`);
}
return await response.json();
}
// Usage
const newContact = await createContact({
name: 'John Doe',
email: 'john@example.com',
phone: '+1234567890'
});
console.log('Created contact:', newContact);Python
import requests
import os
def create_contact(name, email, phone):
url = 'https://api.contactlist.io/v1/contacts'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("CONTACTLIST_API_KEY")}'
}
data = {
'name': name,
'email': email,
'phone': phone
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
# Usage
new_contact = create_contact(
name='John Doe',
email='john@example.com',
phone='+1234567890'
)
print(f'Created contact: {new_contact}')Fetching Contacts
Retrieve all contacts with pagination:
JavaScript/TypeScript
async function getContacts(page = 1, limit = 50) {
const response = await fetch(
`https://api.contactlist.io/v1/contacts?page=${page}&limit=${limit}`,
{
headers: {
'Authorization': `Bearer ${process.env.CONTACTLIST_API_KEY}`
}
}
);
if (!response.ok) {
throw new Error(`Failed to fetch contacts: ${response.statusText}`);
}
return await response.json();
}
// Usage
const contacts = await getContacts(1, 50);
console.log(`Found ${contacts.total} contacts`);
console.log(contacts.data);Updating a Contact
Update an existing contact:
async function updateContact(contactId, updates) {
const response = await fetch(
`https://api.contactlist.io/v1/contacts/${contactId}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.CONTACTLIST_API_KEY}`
},
body: JSON.stringify(updates)
}
);
if (!response.ok) {
throw new Error(`Failed to update contact: ${response.statusText}`);
}
return await response.json();
}
// Usage
const updated = await updateContact('123', {
name: 'Jane Doe',
email: 'jane@example.com'
});Deleting a Contact
async function deleteContact(contactId) {
const response = await fetch(
`https://api.contactlist.io/v1/contacts/${contactId}`,
{
method: 'DELETE',
headers: {
'Authorization': `Bearer ${process.env.CONTACTLIST_API_KEY}`
}
}
);
if (!response.ok) {
throw new Error(`Failed to delete contact: ${response.statusText}`);
}
return await response.json();
}
// Usage
await deleteContact('123');Next Steps
Now that you've seen the basics, check out our Advanced Examples for more complex use cases.