Skip to content

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.

  • Who it is forBusinesses and developers
  • LevelDecision
  • Reading timeabout 10 minutes
  • Last updated24 October 2026

What you will learn

  • When a file exchange is enough and when an API is needed
  • Which four criteria the decision rests on
  • Which five traps the CSV format contains and how an import catches them
  • Which combinations of file and API hold up in practice

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:

The four criteria rarely all point the same way. What usually decides is the one whose failure costs most in day-to-day operation.
Criterionpoints to a filepoints to an API
FreshnessOne snapshot a day or an hour is enough for the process.The process waits for the result, such as availability or an order number.
VolumeMany records at once, for example a full product catalogue.Individual transactions spread across the day.
Error feedbackAn error log after the run is sufficient; a person works through it.The sender must know straight away whether a specific record was accepted.
OwnershipThe 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:

  • Separators. RFC 4180 specifies the comma. Spreadsheet programs such as Excel often write a semicolon in locales where the comma is the decimal mark, such as German. An import expecting the wrong character reads each line as a single field.
  • Encoding. Whether an “é” is stored as one byte (Windows-1252) or two (UTF-8) cannot be seen from the file. Some programs put a byte order mark (BOM, three invisible bytes) at the start of a UTF-8 file. If it is not removed, the first column can no longer be found by its name.
  • Numbers and dates. “12,90” and “12.90”, “03.02.2026” and “02/03/2026” – without an agreement it is unclear whether 3 February or 2 March is meant. An import should accept exactly one format and reject everything else.
  • Conversion on opening. Spreadsheet programs reinterpret values when a file is opened: leading zeros in product codes or postcodes can vanish, long numbers can end up in scientific notation, text such as “1-2” can be read as a date. Once the file is saved, the change is permanent.
  • Line breaks and quotes inside fields. Under RFC 4180, fields containing separators, quotes or line breaks are enclosed in double quotes, and a quote inside a field is doubled. An import that simply splits the file line by line tears multi-line descriptions apart.

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.

PHPPHP 8.3–8.5 · tested with PHP 8.4 CLI
<?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'));
}
Output: 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:

  • A file delivered through an API. The partner uploads the file with a request and is told at once whether header and format are correct. The contents are then processed in the background.
  • Nightly full snapshot plus individual changes. A file brings the complete state every night; during the day, only changes arrive via an API or webhook. The difference between polling and being notified is explained in REST API and webhook.
  • Import with a feedback file. Each run produces a second file listing the rejected lines and their reasons, which goes back to the sender. The file route gains error feedback without becoming an API.
  • Step by step. The exchange starts with a file because it is quick to set up. If freshness or feedback turn out to be insufficient, the affected part moves to an API.

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

  1. How old may the data in the target system be before someone makes a wrong decision?
  2. Does the sender need to know for each record whether it was accepted – or is a list of rejections enough?
  3. Can the other side offer an API at all, and who looks after it there?
  4. Is there an agreed file format, or is the file currently put together by hand in a spreadsheet?
  5. 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

RFC 4180 describes a widespread format but was published as an informational document. Separators, encodings and number formats differ between programs and have to be agreed for each connection.

It is the more current and more communicative solution. If a daily snapshot is enough and the other side has nobody to run an API, a properly checked file is often the more robust choice.

Opening it can reinterpret leading zeros, long numbers and date-like text. Once saved, those changes are part of the file.

Further sources

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.