Your task definition is where you'll perform your tasks, such as sending emails,
processing files, or moving data between systems.With Mergent, tasks are defined by HTTP request handlers inside your
application.
server.ml
Copy
open Lwt.Infixopen Cohttpopen Cohttp_lwt_unixlet perform_task () = (* This is where you'll perform your task. For now, we'll just log it. *) Lwt_io.printf "Performing task: %s\n"let server = let callback _conn req _body = let uri = req |> Request.uri |> Uri.to_string in match Uri.path (Request.uri req) with | "/api/tasks" -> begin perform_task () >>= fun () -> Server.respond_string ~status:`OK ~body:"" () end | _ -> Server.respond_string ~status:`Not_found ~body:"Route not found" () in Server.create ~mode:(`TCP (`Port 3000)) (Server.make ~callback ())
For a list of all the available parameters, visit the API
Reference.
Before you can run your task, your handler must be accessible from the internet.To do this, you can use a tool like ngrok, or deploy your
application to a platform like Vercel or Render.For more information, see our guide to localhost dev &
webhooks.
Once your task handler is reachable via a URL, you can create your first task.
Go to the Mergent Tasks Dashboard and click the
Create button.
In this step, you'll set the Request URL to your task handler's URL. You'll also
provide the required parameters (in the request body) for your handler to carry
out the task.
Typically, developers use a type parameter to distinguish between different
tasks. This allows your task handler to route and process tasks accordingly.
Feel free to structure your parameters in a way that works best for your
application.
Click Create to queue the task. Once the task executes, you should see the
related log in your console, indicating a successful run. 🎊
Create a task whenever you need to. For example, you might create a task
any time a user signs up.Use the URL of the task definition you'd like to run.
Copy
open Lwt.Infixopen Cohttpopen Cohttp_lwt_unixlet create_task () = (* set the Mergent API key *) let api_key = "..." in (* create a task that will run in 5 minutes *) (* the URL should be set to the URL of your task handler *) let headers = Header.init_with "Authorization" ("Bearer " ^ api_key) in let body = `String "{ \"request\": { \"url\": \"...\", \"body\": \"Hello, world!\" }, \"delay\": \"PT5M\" }" in Client.post ~headers ~body (Uri.of_string "https://api.mergent.co/v2/tasks") >>= fun (resp, body) -> let code = resp |> Response.status |> Code.code_of_status in body |> Cohttp_lwt.Body.to_string >|= fun body -> Lwt_io.printf "Response: %d\n%s\n" code body
You can now create a task any time you'd like to perform some work in the
background. 🎊