Hey there, fellow developer! Ready to supercharge your landing page game with Landingi's API? Let's dive into building a robust C# integration that'll have you creating, managing, and analyzing landing pages like a pro.
Before we jump in, make sure you've got:
Fire up your IDE and let's get this show on the road:
Install-Package Newtonsoft.Json
Install-Package RestSharp
Landingi uses API key authentication. Let's create a base client to handle this:
public class LandingiClient { private readonly RestClient _client; private readonly string _apiKey; public LandingiClient(string apiKey) { _apiKey = apiKey; _client = new RestClient("https://api.landingi.com/v1/"); } protected RestRequest CreateRequest(string resource, Method method) { var request = new RestRequest(resource, method); request.AddHeader("Authorization", $"ApiKey {_apiKey}"); return request; } // We'll add more methods here soon! }
Now, let's implement some key operations:
public async Task<List<LandingPage>> GetLandingPagesAsync() { var request = CreateRequest("landing_pages", Method.GET); var response = await _client.ExecuteAsync<List<LandingPage>>(request); return response.Data; }
public async Task<LandingPage> CreateLandingPageAsync(string name, string templateId) { var request = CreateRequest("landing_pages", Method.POST); request.AddJsonBody(new { name, template_id = templateId }); var response = await _client.ExecuteAsync<LandingPage>(request); return response.Data; }
public async Task<LandingPage> UpdateLandingPageAsync(string pageId, string name) { var request = CreateRequest($"landing_pages/{pageId}", Method.PUT); request.AddJsonBody(new { name }); var response = await _client.ExecuteAsync<LandingPage>(request); return response.Data; }
public async Task DeleteLandingPageAsync(string pageId) { var request = CreateRequest($"landing_pages/{pageId}", Method.DELETE); await _client.ExecuteAsync(request); }
RestSharp handles a lot of the heavy lifting, but let's add some error handling:
private void HandleResponse(IRestResponse response) { if (!response.IsSuccessful) { throw new ApiException(response.StatusCode, response.Content); } }
Add this to each method before returning the response.
public async Task<List<FormSubmission>> GetFormSubmissionsAsync(string pageId) { var request = CreateRequest($"landing_pages/{pageId}/submissions", Method.GET); var response = await _client.ExecuteAsync<List<FormSubmission>>(request); HandleResponse(response); return response.Data; }
Don't forget to test! Here's a quick unit test example:
[Fact] public async Task GetLandingPages_ReturnsListOfPages() { var client = new LandingiClient("your-api-key"); var pages = await client.GetLandingPagesAsync(); Assert.NotNull(pages); Assert.True(pages.Count > 0); }
And there you have it! You've just built a solid foundation for your Landingi API integration. Remember, this is just the beginning - there's so much more you can do with the API. Keep exploring, keep coding, and most importantly, keep creating awesome landing pages!
Happy coding, and may your conversion rates always be high! 🚀