@@ -3,7 +3,7 @@ title: Code examples for Serverless Functions
33description : Explore code examples for creating and deploying Serverless Functions in Scaleway.
44tags : functions serverless serverless-functions
55dates :
6- validation : 2025-02-27
6+ validation : 2025-09-02
77 posted : 2021-10-18
88---
99import Requirements from ' @macros/iam/requirements.mdx'
@@ -107,7 +107,7 @@ def handle(event, context):
107107
108108Example of reading URL parameters:
109109
110- ```
110+ ``` sh
111111curl https://myfunc/user/? id=1
112112```
113113
@@ -212,110 +212,111 @@ There are multiple ways a response can return from a handler function:
212212
213213- With body and statusCode. This response type sets the status code as ` HTTP Response Code ` , and body as the response's body, headers as ` Headers ` .
214214
215- ** Stringified * body* (like ` AWS Lambda ` ):**
215+ ** Stringified * body* (like ` AWS Lambda ` ):**
216216
217- ``` js
218- module .exports .myHandler = (event , context , callback ) => {
219- return {
220- statusCode: 201 ,
221- body: JSON .stringify ({
222- message: " async function" ,
223- }),
224- headers: {
225- " Content-Type" : " application/json" ,
226- },
227- }
228- }
229- ```
217+ ``` js
218+ module .exports .myHandler = (event , context , callback ) => {
219+ return {
220+ statusCode: 201 ,
221+ body: JSON .stringify ({
222+ message: " async function" ,
223+ }),
224+ headers: {
225+ " Content-Type" : " application/json" ,
226+ },
227+ }
228+ }
229+ ```
230230
231- *** Not* Stringified ** body** (like ` AWS Lambda ` ):**
231+ *** Not* Stringified ** body** (like ` AWS Lambda ` ):**
232232
233- ``` js
234- module .exports .myHandler = (event , context , callback ) => {
235- return {
236- statusCode: 201 ,
237- body: {
238- message: " async function" ,
239- },
240- headers: {
241- " Content-Type" : " application/json" ,
242- },
243- }
244- }
245- ```
233+ ``` js
234+ module .exports .myHandler = (event , context , callback ) => {
235+ return {
236+ statusCode: 201 ,
237+ body: {
238+ message: " async function" ,
239+ },
240+ headers: {
241+ " Content-Type" : " application/json" ,
242+ },
243+ }
244+ }
245+ ```
246246
247247- With object/entity (number, boolean, string...) without properties body and statusCode
248- ``` js
249- module .exports .myHandler = (event , context , callback ) => {
250- return {
251- body: " message" ,
252- headers: {
253- " Content-Type" : [" text/plain" ],
254- },
255- };
256- // Or
257- return JSON .stringify ({ message: " message" });
258- // OR
259- return " Hello, world" ;
260- // OR
261- return 1 ; // true, false, undefined, null...
262- };
263- ```
264-
265- ** Using the callback parameter:**
266248
267- ``` js
268- module .exports .myHandler = (event , context , callback ) => {
269- const response = {
270- statusCode: 201 ,
271- body: {
272- message: " async function" ,
273- },
274- headers: {
275- " Content-Type" : " application/json" ,
276- },
277- };
278- // Successful response
279- callback (undefined , response);
280- // Error response
281- callback (new Error (" something bad happened..." ));
282- };
283- ```
249+ ``` js
250+ module .exports .myHandler = (event , context , callback ) => {
251+ return {
252+ body: " message" ,
253+ headers: {
254+ " Content-Type" : [" text/plain" ],
255+ },
256+ };
257+ // Or
258+ return JSON .stringify ({ message: " message" });
259+ // OR
260+ return " Hello, world" ;
261+ // OR
262+ return 1 ; // true, false, undefined, null...
263+ };
264+ ```
265+
266+ ** Using the callback parameter:**
267+
268+ ``` js
269+ module .exports .myHandler = (event , context , callback ) => {
270+ const response = {
271+ statusCode: 201 ,
272+ body: {
273+ message: " async function" ,
274+ },
275+ headers: {
276+ " Content-Type" : " application/json" ,
277+ },
278+ };
279+ // Successful response
280+ callback (undefined , response);
281+ // Error response
282+ callback (new Error (" something bad happened..." ));
283+ };
284+ ```
284285
285286- With a ` Promise ` :
286287
287- ``` js
288- module .exports .myHandler = async (event , context , callback ) => {
289- return {
290- statusCode: 201 ,
291- body: {
292- message: " async function" ,
293- },
294- headers: {
295- " Content-Type" : " application/json" ,
296- },
297- };
298- }
288+ ``` js
289+ module .exports .myHandler = async (event , context , callback ) => {
290+ return {
291+ statusCode: 201 ,
292+ body: {
293+ message: " async function" ,
294+ },
295+ headers: {
296+ " Content-Type" : " application/json" ,
297+ },
298+ };
299+ }
299300
300- // OR
301- module .exports .myHandler = (event , context , callback ) => {
302- const response = {
303- statusCode: 201 ,
304- body: {
305- message: " async function" ,
306- },
307- headers: {
308- " Content-Type" : " application/json" ,
309- },
310- };
301+ // OR
302+ module .exports .myHandler = (event , context , callback ) => {
303+ const response = {
304+ statusCode: 201 ,
305+ body: {
306+ message: " async function" ,
307+ },
308+ headers: {
309+ " Content-Type" : " application/json" ,
310+ },
311+ };
311312
312- return new Promise ((resolve , reject ) => {
313- // do something
314- if (err) return reject (err);
315- return resolve (response);
316- });
317- };
318- ```
313+ return new Promise ((resolve , reject ) => {
314+ // do something
315+ if (err) return reject (err);
316+ return resolve (response);
317+ });
318+ };
319+ ```
319320
320321### Using environment variables in Node
321322
@@ -359,7 +360,7 @@ module.exports.myHandler = (event, context, callback) => {
359360
360361Example of reading URL parameters:
361362
362- ```
363+ ``` sh
363364curl https://myfunc/user/? id=1
364365```
365366
@@ -652,7 +653,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
652653
653654Example of reading URL parameters:
654655
655- ```
656+ ``` sh
656657curl https://myfunc/user/? id=1
657658```
658659
@@ -793,7 +794,7 @@ function handle($event, $context) {
793794
794795Example of reading URL parameters:
795796
796- ```
797+ ``` sh
797798curl https://myfunc/user/? id=1
798799```
799800
0 commit comments