Duty is the platform for minimalist developers, just write a simple Javascript function that completes a task and deploy it. And that's it, you created a duty.
We enable developers to iterate quickly and deploy pieces of code to the edge in a click of a button. Duty takes care of your infrastructure so you can focus on finding solutions and deliver fast responses to large amounts of customers.
Duty is a great fit for:
-
Deploy serverless APIs and microservices in minutes.
-
Automate your workflow with webhooks.
-
Trigger recurring duties with cron jobs.
-
Deploy code to the edge in a click of a button.
-
Connect with a MongoDB database via the MongoDB driver for Node.js.
-
Front-end developers who wants to create applications but don't want to worry about server management.
If you don't have a Duty account yet, you can sign up for free here.
If you don't have a Duty account yet, you can sign up for free here.
Once you've signed up, you can create a project by clicking on the Add project button on your dashboard.
You will need to provide a name for your project and select a unique slug. You will also need to add a description.
The slug is used to identify your project in the URL. For example, if you create a project with the slug my-project
, the endpoints to access it will be https://tryduty.com/api/v1/my-project/{any-duty}
.
Once you've created a project, you can add a duty by clicking on the "Add duty" button on your project details page.
Once you have created a project, you can add duties to it. A Duty is a simple javascript function that lives in an endpoint. It will be executed everytime you call that endpoint.
When you create a Duty you will be asked to provide a name, a description and, again, a unique slug. This slug will be used to identify the duty in your project.
You can test your duty by clicking on the Test function button. This will execute your function and show you the result on the right side of the screen.
Once you have finished testing your duty, you can save it by clicking on the Save function button.
Duties MUST START with the statement module.exports
and export an async function.
Otherwise the duty will fail.
Duties receive one parameter, which is the Request
object. This object contains all the information about the request that was made to the endpoint.
So you can easily access the request body, headers, query parameters, etc. For a full list of the properties of the Request
object, please refer to the Request object documentation.
You can access the environment variables of your project by using the process.env
object. For example, if you have an environment variable called MY_ENV_VAR
, you can access it by using process.env.MY_ENV_VAR
.
API keys are used to authenticate requests to the API. You can create API keys in the API Keys section of the dashboard of your project.
All you need to do is send the key as a header with the name Duty-Token
in your request.
Or you can use the Duty SDK to interact with the API.
All duties get a request object as their first argument. This object contains all the information about the request that was made to the duty.
The URL of the request.
The HTTP method of the request.
The headers of the request.
The raw headers of the request.
The status code of the request.
The body of the request.
The query of the request.
The cookies of the request.
The Duty SDK is a Typescript library that allows you to interact with the Duty API. It is designed to be easy to use. As Duty is thought for React environments, the SDK is designed to be used with React. But can be used with any other framework.
The SDK uses the native fetch API. So you can modify the request body by passing an object as the second argument.
npm install duty-sdk
// configs.ts
import { Client } from 'duty-sdk'
export const api = new Client('api-key', 'project-slug')
import { api } from './configs.ts'
const fetchdata = async () => {
const { data, error } = await api.get('/endpoint')
if (error) {
// handle error
}
return data
}
The only thing that changes is the method name.
import { api } from './configs.ts'
const postdata = async () => {
const { data, error } = await api.post('/endpoint', {
body: { my: 'data' },
})
if (error) {
// handle error
}
return data
}
This is a React Hook that fetches data using React Hooks.
// components.ts
import { api } from './configs.ts'
export const MyComponent = () => {
const { data, error, isLoading } = api.useGet<{ my: 'data' }>('/endpoint')
if (isLoading) return <div>Loading...</div>
return <div>{data}</div>
}