Issues with response from API (Python)


I am using Python to make API requests, but I am getting different errors each time for different things. For example, part of my code is getting a contact. So, I will run my program and then I will get a error from my Python saying that it has returned as None (errors out because I try to manipulate the JSON but will error because None is not JSON). So, I initially thought this was issue with my code, but 2 things: 1.) It should never be None. Making a call to the API will always either return the contact or a 404 error. 2.) Running the code multiple times does not get repeated results. So I will run it once and it will error out on one user, but then when I run it again it will return that contact just fine, but then error out on a different one (all of this without changing my code). So, this has led me to believe this is either an error with Syncro’s API, or maybe something with my headers? I am just not sure why I am not getting consistent results. I think that I have to be missing something here! Below is my Python code (the brackets are where I put in my information. Also, I know this function isn’t the prettiest, so my apologies!) :

def getApi(ending):

api_url = "https://{company_name}{0}".format(ending)
headers = {'Authorization' : '{apiKey}', 'accept': 'application/json'}
response = requests.get(api_url, headers=headers)
data = json.loads(response.text)
return data

Any help would be greatly appreciated.

Try this and see if it makes any difference, bearer is used in the PS API scripts i’ve seen.

headers = {'Authorization' : 'Bearer {apiKey}', 'accept': 'application/json'}

I find that I don’t need to worry about headers when calling the Syncro API
Here is some .NET C# code.

HttpClient client = new HttpClient();
string apiString = "/customers";
Uri uri = new Uri($"https://{company_name}{apiString}?api_key={apiKey}");
client.BaseAddress = uri;
HttpResponseMessage response = client.GetAsync(uri).Result;
if (response.IsSuccessStatusCode)
                using (Stream s = await response.Content.ReadAsStreamAsync())
                using (StreamReader sr = new StreamReader(s))
                using (JsonReader js = new JsonTextReader(sr))
                    syncroCustomers = new JsonSerializer().Deserialize<SyncroCustomers>(js);

I tried this but I am still getting inconsistent errors. Sometimes I run it, works fine. Run the exact same script, errors out based on the response.

I tried using the api key in the url, but it didn’t work. I know I am using python, but the URL should be the same?

Yes, you using the same URIL as I posted should work. Though I suggest removing your headers, though maybe that is needed for Python and not needed for .NET when using HttpClient. I’ve never used Python, so but for me, headers are not required.

Does your API key work as expected on the API Docs page?
SyncroMSP API Docs


Have you tried printing the whole response.text you are getting from the server when you are getting a None result from your request or using a debugger to see what is going on?

It would help if you share the exact error Python is giving you, is it the response.text that is None or the ‘data’?

Yes, it works just fine in the API Docs page, but whenever I try and use the API multiple times is starts acting funky.

The error python is giving me is when I try and manipulate the data. It will work for other users, but then the error will be something in regards to not being able to do anything with None, but the response should never just be None. Furthermore, it will error out on one user, and then on the same user it will work when I run it again. I do not believe this is an issue with Python.

Python programmer here - how are you assuring you are getting the correct data?