CSV import or API integration: which approach fits?
Whether data travels between two systems as a file or through an API is not a matter of taste. Four criteria decide it – and if you choose the file, you should know the places where CSV quietly delivers wrong data.
What you will learn
The answer in three sentences
A file is enough when a snapshot at fixed intervals serves the process, the volume is manageable and an error spotted the next morning is still spotted in time. An API pays off when minutes matter, every single record needs a response, or both sides write to the same data. Very often the solution that holds is a mix of the two.
The general options – files, direct database access, APIs, parsing e-mail – are covered in Connecting Excel, e-mail and business software. This article deals with the narrower question: file or API, and what to watch for if it is a file.
Four criteria that decide
An API (application programming interface) is an agreed point through which one system requests data from another, or delivers it, and receives an immediate answer. What belongs to one is explained in What is an API?. A file exchange, by contrast, drops a file that the other system collects later. The differences show in four places:
| Criterion | points to a file | points to an API |
|---|---|---|
| Freshness | One snapshot a day or an hour is enough for the process. | The process waits for the result, such as availability or an order number. |
| Volume | Many records at once, for example a full product catalogue. | Individual transactions spread across the day. |
| Error feedback | An error log after the run is sufficient; a person works through it. | The sender must know straight away whether a specific record was accepted. |
| Ownership | The partner can only export; nobody on their side looks after an API. | Both sides have someone who runs the connection and announces changes. |
The fourth criterion is the one most often overlooked. An API is a connection someone has to maintain: keys expire, fields are added, the other side updates its software. A file shifts that upkeep into a simple agreement on columns and format – provided someone notices when the file fails to arrive.
What a file does that is often underrated
A file is a record. It can be kept, imported again and opened when a question comes up. The partner needs no development work, only an export. And a failed run can be repeated with the same file, without anyone having to reconstruct yesterday's data.
Five traps in the CSV format
CSV stands for “comma-separated values”: a text file in which each line is a record and separators divide the fields. The common description is RFC 4180 from 2005. It was published as an informational document, not a binding standard, and many programs deviate from it. Five typical traps follow:
How an import catches these traps
The example below reads the same row from two sources: once as UTF-8 with a BOM and semicolons, as spreadsheet programs often produce it, and once in Windows-1252 from an older system. The naive call with a comma as separator returns a single column preceded by three BOM bytes. The function readCsv() removes the mark, converts the encoding, detects the separator from the header line and interprets the decimal comma and the date explicitly.
<?php
declare(strict_types=1);
// Two exports of the same row: Excel "CSV UTF-8" (BOM, semicolon) and an old
// system writing Windows-1252. Both are "CSV", neither uses a comma.
$utf8 = "\xEF\xBB\xBFsku;name;price;updated\r\n4711;Café Crème;12,90;03.02.2026\r\n";
$legacy = "sku;name;price;updated\r\n4711;Caf\xE9 Cr\xE8me;12,90;03.02.2026\r\n";
// Naive: default separator, BOM left in place.
$naive = str_getcsv(strtok($utf8, "\r\n"), ',', '"', '');
var_dump($naive[0]); // string(25): one column, three BOM bytes in front
function readCsv(string $raw): array
{
$raw = preg_replace('/^\xEF\xBB\xBF/', '', $raw); // strip UTF-8 BOM
if (!mb_check_encoding($raw, 'UTF-8')) {
$raw = mb_convert_encoding($raw, 'UTF-8', 'Windows-1252');
}
$lines = preg_split('/\r\n|\n|\r/', trim($raw));
$sep = substr_count($lines[0], ';') > substr_count($lines[0], ',') ? ';' : ',';
$head = str_getcsv(array_shift($lines), $sep, '"', '');
return array_map(fn (string $l) => array_combine($head, str_getcsv($l, $sep, '"', '')), $lines);
}
foreach ([$utf8, $legacy] as $raw) {
$row = readCsv($raw)[0];
$price = (float) str_replace(',', '.', $row['price']); // decimal comma
$date = DateTimeImmutable::createFromFormat('!d.m.Y', $row['updated']);
printf("%s | %s | %.2f | %s\n", $row['sku'], $row['name'], $price, $date->format('Y-m-d'));
}
string(25) for the naive version (22 characters plus three BOM bytes), then 4711 | Café Crème | 12.90 | 2026-02-03 twice. Detecting the encoding is a fallback: agreeing on it in writing is better.More important than any line of code are three rules for the import. The format is agreed in writing: separator, encoding, number and date format, column names. The header line is checked against the expected columns before a single record is accepted. And every rejected record goes into a log with its line number and reason, which a person actually reads.
Hybrids: file and API together
The choice need not be exclusive. Four combinations come up often:
Combining both means expecting duplicates: a record arrives through the API and again in the next morning's file. How to process that without creating duplicate transactions is covered in Preventing duplicate processing of webhooks.
A hypothetical example
The price list with the wrong separator
A wholesaler sends a weekly price list as a CSV file, which an import loads into the retailer's shop. One Monday the file arrives with commas instead of semicolons, because the supplier exported it from a computer with different regional settings. The import does not stop: it cannot find the “price” column, takes over the products with empty prices and reports success.
The supplier offers no API, so the file remains the right route. Three things change. The import compares the header with the expected columns and rejects the whole file if one is missing. It does not accept a price that is empty or has jumped sharply since the previous week, but holds it back for review. And purchasing receives a short message after each run with the number of lines accepted and rejected.
Five questions to decide
- How old may the data in the target system be before someone makes a wrong decision?
- Does the sender need to know for each record whether it was accepted – or is a list of rejections enough?
- Can the other side offer an API at all, and who looks after it there?
- Is there an agreed file format, or is the file currently put together by hand in a spreadsheet?
- Who notices if no file and no request has arrived for three days?
If the answers on freshness and feedback point one way, an API is the likely choice. If they point to a partner without developers, the file is the realistic option – with the checks described here. If the task is not an ongoing exchange but a one-off transfer from a legacy system, that is a separate topic: Planning a data migration. We support both routes as part of our API integration work.
Common misconceptions
Further sources
- RFC 4180 – Common Format and MIME Type for Comma-Separated Values (CSV) Files description of the common CSV format, Informational, October 2005
- PHP Manual – fgetcsv parameters, locale note and the PHP 8.4 change
- PHP Manual – str_getcsv parsing a CSV string, changes in PHP 8.4
- Microsoft Support – Keeping leading zeros and large numbers Excel drops leading zeros, shows long numbers in scientific notation and converts date-like values
About the author
Fabian Lücker is managing partner of PHP Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and design, and writes here about decisions that come up in real projects.
File or API?
Describe which data moves between your systems today, how it gets there and where it goes wrong. We will assess which route suits your process.