Hey there, fellow developer! Ready to supercharge your forms with the 123FormBuilder API? Let's dive in and build a slick C# integration that'll have you managing forms like a pro in no time.
Before we jump into the code, make sure you've got:
Fire up Visual Studio and create a new C# project. We'll be using a console app for this guide, but feel free to adapt it to your needs.
Next, let's grab the necessary NuGet packages:
Install-Package Newtonsoft.Json
Install-Package RestSharp
Alright, let's get that API key working for us. Create a new class called FormBuilderClient
:
public class FormBuilderClient { private readonly string _apiKey; private readonly RestClient _client; public FormBuilderClient(string apiKey) { _apiKey = apiKey; _client = new RestClient("https://api.123formbuilder.com/v2"); } // We'll add more methods here soon! }
Now for the fun part - let's interact with some forms!
public async Task<List<Form>> GetFormsAsync() { var request = new RestRequest("forms", Method.GET); request.AddHeader("Authorization", $"Bearer {_apiKey}"); var response = await _client.ExecuteAsync<List<Form>>(request); return response.Data; }
public async Task<Form> GetFormAsync(int formId) { var request = new RestRequest($"forms/{formId}", Method.GET); request.AddHeader("Authorization", $"Bearer {_apiKey}"); var response = await _client.ExecuteAsync<Form>(request); return response.Data; }
public async Task<bool> SubmitFormAsync(int formId, Dictionary<string, string> formData) { var request = new RestRequest($"forms/{formId}/submissions", Method.POST); request.AddHeader("Authorization", $"Bearer {_apiKey}"); request.AddJsonBody(formData); var response = await _client.ExecuteAsync(request); return response.IsSuccessful; }
To stay on top of form submissions in real-time, set up a webhook:
public async Task<bool> SetupWebhookAsync(int formId, string webhookUrl) { var request = new RestRequest($"forms/{formId}/webhooks", Method.POST); request.AddHeader("Authorization", $"Bearer {_apiKey}"); request.AddJsonBody(new { url = webhookUrl }); var response = await _client.ExecuteAsync(request); return response.IsSuccessful; }
For forms with file uploads, you'll need to handle multipart form data:
public async Task<bool> SubmitFormWithFileAsync(int formId, Dictionary<string, string> formData, string filePath) { var request = new RestRequest($"forms/{formId}/submissions", Method.POST); request.AddHeader("Authorization", $"Bearer {_apiKey}"); foreach (var item in formData) { request.AddParameter(item.Key, item.Value); } request.AddFile("file", filePath); var response = await _client.ExecuteAsync(request); return response.IsSuccessful; }
Always wrap your API calls in try-catch blocks to handle exceptions gracefully:
try { var forms = await formBuilderClient.GetFormsAsync(); } catch (Exception ex) { Console.WriteLine($"Oops! Something went wrong: {ex.Message}"); }
And don't forget about rate limiting! The 123FormBuilder API has limits, so be a good citizen and add some delay between requests if you're making multiple calls.
Unit test your FormBuilderClient
methods to ensure they're working as expected. For end-to-end testing, create a test form on 123FormBuilder and use its ID in your integration tests.
When deploying your integration:
And there you have it! You've just built a robust 123FormBuilder API integration in C#. With these building blocks, you can create, manage, and analyze forms like a champ. Remember, the 123FormBuilder API docs are your best friend for diving deeper into specific endpoints and features.
Now go forth and build some awesome form-powered applications! 🚀