Skip to main content

REST Console Sample

The ECGrid-REST-dotnet10-Console project is the simplest starting point for ECGrid REST integration. It walks through a complete file exchange workflow using a .NET 10 console application with top-level statements.

Project Location

samples/rest/ECGrid-REST-dotnet10-Console/

What It Demonstrates

This sample covers the core EDI file exchange workflow end to end:

  1. Check version — confirm connectivity and API version
  2. Check inbox — list parcels waiting for download
  3. Download parcel — retrieve the EDI file content
  4. Confirm download — acknowledge receipt so ECGrid marks the parcel delivered
  5. Upload file — send an outbound EDI file to a trading partner

Key Files

FilePurpose
Program.csEntry point — all workflow logic using top-level statements
appsettings.jsonBase configuration (base URL, mailbox ID, etc.)
appsettings.Development.jsonLocal overrides (excluded from source control)

Configuration

The API key and connection settings are loaded from IConfiguration. Add your API key via user-secrets to avoid committing credentials:

cd samples/rest/ECGrid-REST-dotnet10-Console
dotnet user-secrets set "ECGrid:ApiKey" "your-key-here"

The appsettings.json structure used by the sample:

{
"ECGrid": {
"BaseUrl": "https://rest.ecgrid.io",
"ApiKey": "",
"MailboxId": 0
}
}

Key Patterns

HttpClient Setup

The sample configures HttpClient through the host builder so the API key is applied to every request automatically:

// Program.cs — .NET 10 top-level statements
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = Host.CreateApplicationBuilder(args);

host.Services.AddHttpClient("ecgrid", (sp, client) =>
{
var config = sp.GetRequiredService<IConfiguration>();
client.BaseAddress = new Uri(config["ECGrid:BaseUrl"]!);
// API key authentication — key loaded from IConfiguration, never hardcoded
client.DefaultRequestHeaders.Add("X-API-Key", config["ECGrid:ApiKey"]!);
});

var app = host.Build();
var httpFactory = app.Services.GetRequiredService<IHttpClientFactory>();
var http = httpFactory.CreateClient("ecgrid");

Inbox Check and Download Loop

// Check inbox and process each waiting parcel
var inboxResponse = await http.PostAsJsonAsync(
"v2/parcels/pending-inbox-list", new { mailboxId, pageNo = 1, recordsPerPage = 25 });
var inbox = await inboxResponse.Content.ReadFromJsonAsync<ApiResponse<List<ParcelSummary>>>();

foreach (var parcel in inbox?.Data ?? [])
{
// Download the parcel content
var download = await http.GetAsync($"/v2/parcels/{parcel.ParcelId}/download");
var ediContent = await download.Content.ReadAsByteArrayAsync();

// Save locally — production code would route to a processor
await File.WriteAllBytesAsync($"parcel-{parcel.ParcelId}.edi", ediContent);

// Confirm receipt so ECGrid marks the parcel as delivered
await http.PostAsync($"/v2/parcels/{parcel.ParcelId}/confirm", null);
}

Upload

// Upload an outbound EDI file
var fileBytes = await File.ReadAllBytesAsync("outbound.edi");
using var content = new ByteArrayContent(fileBytes);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

var uploadResult = await http.PostAsync("/v2/parcels/upload", content);
uploadResult.EnsureSuccessStatusCode();

How to Run

cd samples/rest/ECGrid-REST-dotnet10-Console
dotnet user-secrets set "ECGrid:ApiKey" "your-key-here"
dotnet run

See Also