Hey there, fellow developer! Ready to dive into the world of Microsoft Dynamics 365 ERP API integration? You're in for a treat. This powerful API opens up a whole new realm of possibilities for your applications, allowing you to tap into the robust features of Microsoft's ERP system. Let's get our hands dirty and build something awesome!
Before we jump in, make sure you've got these basics covered:
requests
, json
(You know the drill!)First things first - let's get you authenticated:
import requests client_id = 'your_client_id' client_secret = 'your_client_secret' tenant_id = 'your_tenant_id' token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' scope = 'https://api.businesscentral.dynamics.com/.default' data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': scope } response = requests.post(token_url, data=data) access_token = response.json()['access_token']
Now that we're authenticated, let's set up our base connection:
base_url = 'https://api.businesscentral.dynamics.com/v2.0/your_tenant_id/your_environment/api/v2.0' headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' }
Time for the fun part - let's interact with the API!
response = requests.get(f'{base_url}/companies', headers=headers) companies = response.json()['value']
new_customer = { "displayName": "Contoso, Ltd.", "type": "Company", "addressLine1": "123 Main St" } response = requests.post(f'{base_url}/customers', headers=headers, json=new_customer)
update_data = {"phoneNumber": "555-0123"} response = requests.patch(f'{base_url}/customers(customer_id)', headers=headers, json=update_data)
response = requests.delete(f'{base_url}/customers(customer_id)', headers=headers)
Always check your responses! Here's a quick helper function:
def handle_response(response): if response.status_code == 200: return response.json() else: print(f"Error: {response.status_code}") print(response.text) return None
Want to get fancy with your queries? Try this:
filter_query = "$filter=displayName eq 'Contoso, Ltd.'" response = requests.get(f'{base_url}/customers?{filter_query}', headers=headers)
For when you need to make multiple calls efficiently:
batch_endpoint = f'{base_url}/$batch' batch_requests = [ {"method": "GET", "url": "/customers"}, {"method": "GET", "url": "/vendors"} ] response = requests.post(batch_endpoint, headers=headers, json={"requests": batch_requests})
Always test your API calls! Here's a simple unit test example:
import unittest class TestDynamicsAPI(unittest.TestCase): def test_get_companies(self): response = requests.get(f'{base_url}/companies', headers=headers) self.assertEqual(response.status_code, 200) if __name__ == '__main__': unittest.main()
And there you have it! You're now equipped to build robust integrations with Microsoft Dynamics 365 ERP API. Remember, the API documentation is your best friend for diving deeper into specific endpoints and features.
Now go forth and build something amazing! Happy coding!