{"id":29875751,"url":"https://github.com/crcrc/strapirestclient","last_synced_at":"2025-07-31T02:43:17.058Z","repository":{"id":301615492,"uuid":"1009515280","full_name":"crcrc/StrapiRestClient","owner":"crcrc","description":"a C# .NET Rest Client for Strapi v5","archived":false,"fork":false,"pushed_at":"2025-07-09T11:15:34.000Z","size":10930,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-22T06:22:29.688Z","etag":null,"topics":["csharp","rest-api","strapi","strapi-cms"],"latest_commit_sha":null,"homepage":"https://www.toodle.uk/opensource","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crcrc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-27T08:59:31.000Z","updated_at":"2025-07-09T11:15:39.000Z","dependencies_parsed_at":"2025-06-27T20:42:50.298Z","dependency_job_id":null,"html_url":"https://github.com/crcrc/StrapiRestClient","commit_stats":null,"previous_names":["crcrc/strapirestclient"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/crcrc/StrapiRestClient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crcrc%2FStrapiRestClient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crcrc%2FStrapiRestClient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crcrc%2FStrapiRestClient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crcrc%2FStrapiRestClient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crcrc","download_url":"https://codeload.github.com/crcrc/StrapiRestClient/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crcrc%2FStrapiRestClient/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267978349,"owners_count":24175254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","rest-api","strapi","strapi-cms"],"created_at":"2025-07-31T02:43:13.492Z","updated_at":"2025-07-31T02:43:17.024Z","avatar_url":"https://github.com/crcrc.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C# .NET StrapiRestClient Usage Guide\n\nStrapiRestClient is a C# .NET client specifically designed for **Strapi v5** to get content via the REST API from your Strapi instance.\n\nThis guide provides instructions and examples on how to implement and use the `StrapiRestClient` library in your .NET applications.\n\n## Running the tests\n\nTo run the tests, in the \"strapi-test-instance\" is a demo instance with sqlite database populated with data\n\n- npm install\n- npm run dev\n\nStrapi should be available on http://localhost:1337\n\n- email: test@test.com\n- password: Test1234\n\nSee the StrapiRestClient.Tests/StrapiRestClientTests.cs file for guidance on how to make requests to the Strapi API.\n\n\n## 1. Installation\n\nFirst, add the `StrapiRestClient` NuGet package to your project:\n\n```bash\ndotnet add package StrapiRestClient\n```\n\n## 2. Configuration\n\n### a. `appsettings.json`\n\nAdd your Strapi API base URL and optional API key to your `appsettings.json` file:\n\n```json\n{\n  \"StrapiRestClient\": {\n    \"BaseUrl\": \"http://localhost:1337/api\",\n    \"ApiKey\": \"your-strapi-api-key-if-any\"\n  }\n}\n```\n\n### b. Dependency Injection (`Program.cs` or `Startup.cs`)\n\nRegister the `StrapiRestClient` in your `Program.cs` (for .NET 6+ minimal APIs) or `Startup.cs` (for older .NET versions) using the provided extension method:\n\n```csharp\n// Program.cs\nusing StrapiRestClient.Extensions;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Add StrapiRestClient\nservices.AddStrapiRestClient(configuration);\n\n// ... other services\n\nvar app = builder.Build();\n\n// ...\n\napp.Run();\n```\n\n## 3. Usage Examples\n\nOnce configured, you can inject `IStrapiRestClient` into your services or controllers and use it to interact with your Strapi API.\n\n### a. Defining Your Models\n\nBefore making requests, define C# classes that match the structure of your Strapi v5 content types. **Strapi v5** uses a flatter structure with `documentId` instead of nested attributes:\n\n```csharp\npublic class Article\n{\n    public string? DocumentId { get; set; }\n    public string? Title { get; set; }\n    public string? Description { get; set; }\n    public string? Slug { get; set; }\n    public DateTime CreatedAt { get; set; }\n    public DateTime UpdatedAt { get; set; }\n    public DateTime PublishedAt { get; set; }\n    public Category? Category { get; set; }  // Populated relation\n    public Author? Author { get; set; }      // Populated relation\n}\n\npublic class Category\n{\n    public int Id { get; set; }\n    public string? DocumentId { get; set; }\n    public string? Name { get; set; }\n    public string? Slug { get; set; }\n    public DateTime CreatedAt { get; set; }\n    public DateTime UpdatedAt { get; set; }\n    public DateTime PublishedAt { get; set; }\n}\n```\n\n### b. Making GET Requests\n\n#### Get All Entries\n\n```csharp\nusing StrapiRestClient.Request;\nusing StrapiRestClient.RestClient;\nusing StrapiRestClient.Models;\n\npublic class MyService\n{\n    private readonly IStrapiRestClient _strapiRestClient;\n\n    public MyService(IStrapiRestClient strapiRestClient)\n    {\n        _strapiRestClient = strapiRestClient;\n    }\n\n    public async Task\u003cList\u003cArticle\u003e?\u003e GetArticlesAsync()\n    {\n        var request = new StrapiQueryRequest(\"articles\");\n\n        var response = await _strapiRestClient.ExecuteAsync\u003cICollection\u003cArticle\u003e\u003e(request);\n    }\n}\n```\n\n#### Get Entry by Document ID (Strapi v5)\n\n```csharp\nvar request = new StrapiQueryRequest(\"articles\")\n                   .AddFilter(\"id\", 6)\n                   .WithPopulateAll();\n\nvar response = await _strapiRestClient.ExecuteAsync\u003cICollection\u003cArticle\u003e\u003e(request);\n\nif (response.IsSuccess)\n{\n    Console.WriteLine($\"Article Title: {response.Data?.First()?.Title}\");\n}\n```\n\n#### Get Entry by Slug\n\n```csharp\nvar request = new StrapiQueryRequest(\"articles\")\n                   .AddFilter(\"slug\", \"beautiful-picture\")\n                   .WithPopulateAll();\n\nvar response = await _strapiRestClient.ExecuteAsync\u003cICollection\u003cArticle\u003e\u003e(request);\n\nif (response.IsSuccess)\n{\n    Console.WriteLine($\"Article Title: {response.Data?.First()?.Title}\");\n}\n```\n\n#### Request Methods\n```csharp\n// Root level - \"With\" prefix for setting overall request options\nWithPopulateAll()     // populate=*\nWithFields()          // fields[]=...\nWithSort()            // sort[]=...\nWithPagination()      // pagination[...]\n\n// Adding specific items - \"Add\" prefix for adding to collections\nAddPopulate(relation)        // populate[relation]\nAddPopulateAll(relation)     // populate[relation][populate]=*\nAddFilter(field, value)      // filters[field]=value\nAddRelationFilter()          // filters[relation][field]=value\n```\n\n\n#### Debug URL Generation\n\nUse the new `GetQueryUrl` method to see exactly what URL will be generated:\n\n```csharp\nvar firstPageRequest = new StrapiQueryRequest(\"articles\")\n                                        .WithPagination(page: 1, pageSize: 1)\n                                        .WithSort(\"id:asc\");\n\nvar query = firstPageRequest.ToQueryString();\n//sort[0]=id%3aasc\u0026pagination[pageSize]=1\u0026pagination[page]=1\n\nvar url = firstPageRequest.ToUrl();\n//http://localhost:1337/articles?sort[0]=id%3aasc\u0026pagination[pageSize]=1\u0026pagination[page]=1\n\n```\n\n#### Filtering Data\n\nUse the fluent `WithFilter` method to apply various filters. You can chain multiple filters.\n\n```csharp\nvar request = new StrapiQueryRequest(\"articles\")\n                    .AddFilter(\"title\", FilterBuilder.ContainsCaseInsensitive(\"internet\"))\n                    .AddRelationFilter(\"author\", \"name\", FilterBuilder.Contains(\"David\"))\n                    .AddPopulate(\"author\", new PopulateOptions { Fields = new[] { \"name\", \"email\" } })\n                    .AddPopulate(\"category\", new PopulateOptions { Fields = new[] { \"name\", \"slug\" } })\n                    .WithFields(\"title\", \"slug\", \"publishedAt\", \"description\")\n                    .WithSort(\"publishedAt:desc\")\n                    .WithPagination(page: 1, pageSize: 10)\n                    .WithStatus(\"published\")\n                    .WithLocale(\"en\");\n\nvar response = await _strapiRestClient.ExecuteAsync\u003cICollection\u003cArticle\u003e\u003e(request);\n```\n\n#### Sorting Data\n\nUse `WithSort` to order your results.\n\n```csharp\nvar request = new StrapiQueryRequest(\"articles\")\n                    .WithSort(\"publishedAt:desc\");\n\nvar response = await _strapiRestClient.ExecuteAsync\u003cICollection\u003cArticle\u003e\u003e(request);\n```\n\n#### Pagination\n\nUse `WithPage` and `WithPageSize` for pagination.\n\n```csharp\nvar firstPageRequest = new StrapiQueryRequest(\"articles\")\n                            .WithPagination(page: 1, pageSize: 1)\n                            .WithSort(\"id:asc\");\n```\n\n#### Populating Relations\n\nUse `WithPopulate` to load related data.\n\n```csharp\n//Populate All - all top level data\nvar request = new StrapiQueryRequest(\"articles\")\n                        .AddFilter(\"id\", 6)\n                        .WithPopulateAll();\n\n//Populate all fields in relation\nvar request = new StrapiQueryRequest(\"articles\")\n                    .AddPopulate(\"author\", new PopulateOptions { Fields = new[] { \"*\" } });\n\n//Specific fields\nvar request = new StrapiQueryRequest(\"articles\")\n                           .AddPopulate(\"category\", new PopulateOptions\n                           {\n                               Fields = new[] { \"name\", \"slug\" }\n                           });\n\n//Populate dynamic blocks\nvar request = new StrapiQueryRequest(\"articles\")\n                            .AddPopulateAll(\"blocks\");\n```\n\n\n## 4. Error Handling\n\nThe `ExecuteAsync` method returns a `StrapiResponse\u003cT\u003e` object, which provides comprehensive information about the API call's outcome.\n\n-   **`response.IsSuccess`**: A boolean indicating if the HTTP status code was in the 2xx range.\n-   **`response.Data`**: Contains the deserialized data if `IsSuccess` is `true`. Otherwise, it will be `null`.\n-   **`response.Error`**: Contains detailed error information (status, name, message, details) if `IsSuccess` is `false`. Otherwise, it will be `null`.\n-   **`response.StatusCode`**: The raw HTTP status code returned by the Strapi API.\n\nAlways check `response.IsSuccess` before attempting to access `response.Data`.\n\n```csharp\nvar response = await _strapiRestClient.ExecuteAsync\u003cArticle\u003e(someRequest);\n\nif (response.IsSuccess)\n{\n    // Process successful data\n    var article = response.Data;\n}\nelse\n{\n    // Handle API error\n    Console.WriteLine($\"API Error: {response.Error?.Name} - {response.Error?.Message}\");\n    Console.WriteLine($\"Status Code: {response.StatusCode}\");\n    // You can also inspect response.Error.Details for more specific error information\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrcrc%2Fstrapirestclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrcrc%2Fstrapirestclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrcrc%2Fstrapirestclient/lists"}