Skip to main content

Parallelization

Guidelines for making parallel requests to the MagniFinance API.

Best Practices

1. Concurrent Request Limits

You must keep the number of simultaneous requests under 6.

2. Reacting to Timeouts or Errors

In case a request is met with a timeout or error, implement an exponential backoff strategy:

Retry AttemptWait Time
1st retry5 seconds
2nd retry25 seconds
3rd and following125 seconds

3. Avoid Errors with Early Validations

On requests such as the DocumentCreate and AddPartner with an important field such as Tax Id, that Tax Id will be validated on server side and in case of being invalid an error will be returned.

This is often very late in the process - the user might have already done a registration and left the website, or the service was already provided and there is no easy way to contact the customer. These situations make it hard to correct the data and may put a strain on your business.

To avoid such inconvenience, it is highly recommended that fields like:

  • Tax IDs
  • Post Codes
  • Phone Numbers
  • Tax Percentages

...be validated early in your software or process so that an error is returned to the user and they can easily correct the information.

Code Example

Here's an example of implementing exponential backoff in JavaScript:

async function makeRequestWithRetry(requestFn, maxRetries = 3) {
const delays = [5000, 25000, 125000]; // 5s, 25s, 125s

for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await requestFn();
} catch (error) {
if (attempt === maxRetries) {
throw error;
}

const delay = delays[Math.min(attempt, delays.length - 1)];
console.log(`Request failed, retrying in ${delay/1000} seconds...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}

// Usage
const result = await makeRequestWithRetry(async () => {
const response = await fetch('https://bo.magnifinance.com/api/v1.1/document', {
method: 'POST',
headers: {
'email': 'api@example.com',
'token': 'YOUR_TOKEN',
'Content-Type': 'application/json'
},
body: JSON.stringify(documentData)
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
});

C# Example

public async Task<T> MakeRequestWithRetry<T>(Func<Task<T>> requestFunc, int maxRetries = 3)
{
int[] delays = { 5000, 25000, 125000 }; // 5s, 25s, 125s

for (int attempt = 0; attempt <= maxRetries; attempt++)
{
try
{
return await requestFunc();
}
catch (Exception ex)
{
if (attempt == maxRetries)
{
throw;
}

int delay = delays[Math.Min(attempt, delays.Length - 1)];
Console.WriteLine($"Request failed, retrying in {delay/1000} seconds...");
await Task.Delay(delay);
}
}

throw new InvalidOperationException("Should not reach here");
}
tip

For high-volume integrations, contact us about custom rate limits and best practices for your specific use case.