In this tutorial, we will see how to build a simple API with Fastify and TypeScript, and how to deploy it on the ScaleDynamics Platform.
Fastify is a Node.js web framework for building HTTP APIs. It is designed to be easy to use, and to be fast. It has a powerful plugin architecture inspired by Express and Hapi. It is design with developper experience in mind without sacrificing performance.
Our goal will be to build a simple starting point for a Fastify API with TypeScript that will return some users fetched from jsonplaceholder. Then we will deploy it on the ScaleDynamics Platform to be able to use it from any application.
To follow along, you will need:
Let's create a folder and initialize a npm:
mkdir fastify-api
yarn init
Don't bother with the options in the prompt, you can directly use this code in your package.json file:
Install the dependencies by running:
Now we need to create a tsconfig.json to configure TypeScript, so let's run:
npx tsc --init
We need now to make some changes to it:
{
"compilerOptions": {
"target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */
}
}
The project is now ready, let's build our API:
Create a src folder at the root of your project, and create a index.ts file inside it:
import axios from "axios";
import fastify from "fastify";
const server = fastify();
server.get("/users", async (): Promise<User[]> => {
return (await axios.get("https://jsonplaceholder.typicode.com/users")).data;
});
server.listen(8080, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
});
Our API will simply return a list of users fetched from jsonplaceholder when we call /users.
We need to add the definition of the User type, so create a domain folder and a user.d.ts file inside:
type User = {
id: number;
name: string;
username: string;
email: string;
address: Address;
phone: string;
website: string;
company: Company;
};
type Address = {
street: string;
suite: string;
city: string;
zipcode: string;
geo: Geolocalisation;
};
type Geolocalisation = {
lat: string;
lng: string;
};
type Company = {
name: string;
catchPhrase: string;
bs: string;
};
We can now build our code to transform it into JavaScript and run it:
yarn build
yarn start
Try to get the users by running:
curl localhost:8080/users
You should dump the users like:
Perfect ! Now let's deploy our API on the ScaleDynamics Platform.
To deploy this application on ScaleDynamics's cloud, you need an account and create an environment with a resource for it. The cloud resource provides virtual CPU and storage capacities used to execute the app. In this tutorial we will use a free shared resource available on ScaleDynamics’s cloud. These are the best one for testing. In this tutorial we will see later how you can upgrade to a dedicated production resource on the public cloud provider and region of your choice.
If you don't have an account, feel free to create one here (it's free and no credit card are required). Once your account is created, sign in.
Let's create a project and an environment on the ScaleDynamics's console. Select your organization, create a new project, then create a new environment. Now we need to choose what kind of service we need for our deployment. There are four types:
For our API, we need a server. Let's pick the managed Node.js server. You can learn more on the other types in the ScaleDynamics documentation.
GIF
The environment is ready to run our application, let's deploy on it.
First, let's add a configuration to tell the SDK what type of application we want to deploy. At the root of the project, create a warp.config.js file:
// warp.config.js
module.exports = {
server: "fastify",
};
Build the project to compile the TypeScript code into JavaScript:
yarn build
Log into your account and select your organization via the prompt:
npx warp login
Finally, we can run the deployment command:
npx warp deploy ./
This command will dump something like this:
You can see that a server was detected from your configuration file. Now follow the prompt, select the project you created, then the environment. The prompt will also ask you for a hostname, you can leave it blank for a random name or use the one you want. Finally, you can select a host.
The deployment will take a few minutes.
When it's done, you can open your browser and go to the URL and TADA ! Your API is live !
If you want to use dedicated resources, you can upgrade dynamically to a non shared resource. As shown below it's as simple as a few mouse clicks.
GIF
At this stage, you have a fully functional API. You can learn more on the ScaleDynamics documentation, like how to get the logs of your server, use a custom domain, implement a CI/CD pipeline, etc.
Enjoy !
Expérimentez la plateforme gratuitement avec nos ressources ouvertes à tous
Suivez notre guide de démarrage rapide pour démarrer avec Warp en quelques étapes simples.
Plongez dans les détails techniques