# Snippets ## List zone snippets `client.snippets.list(SnippetListParamsparams, RequestOptionsoptions?): V4PagePaginationArray` **get** `/zones/{zone_id}/snippets` Fetches all snippets belonging to the zone. ### Parameters - `params: SnippetListParams` - `zone_id: string` Path param: Use this field to specify the unique ID of the zone. - `page?: number` Query param: Specifies the current page number. - `per_page?: number` Query param: Specifies how many results to return per page. ### Returns - `SnippetListResponse` Define a snippet. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); // Automatically fetches more pages as needed. for await (const snippetListResponse of client.snippets.list({ zone_id: '9f1839b6152d298aca64c4e906b6d074', })) { console.log(snippetListResponse.created_on); } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": [ { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" } ], "success": true, "result_info": { "count": 25, "page": 1, "per_page": 25, "total_count": 100, "total_pages": 10 } } ``` ## Get a zone snippet `client.snippets.get(stringsnippetName, SnippetGetParamsparams, RequestOptionsoptions?): SnippetGetResponse` **get** `/zones/{zone_id}/snippets/{snippet_name}` Fetches a snippet belonging to the zone. ### Parameters - `snippetName: string` Identify the snippet. - `params: SnippetGetParams` - `zone_id: string` Use this field to specify the unique ID of the zone. ### Returns - `SnippetGetResponse` Contain the response result. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const snippet = await client.snippets.get('my_snippet', { zone_id: '9f1839b6152d298aca64c4e906b6d074', }); console.log(snippet.created_on); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" }, "success": true } ``` ## Update a zone snippet `client.snippets.update(stringsnippetName, SnippetUpdateParamsparams, RequestOptionsoptions?): SnippetUpdateResponse` **put** `/zones/{zone_id}/snippets/{snippet_name}` Creates or updates a snippet belonging to the zone. ### Parameters - `snippetName: string` Identify the snippet. - `params: SnippetUpdateParams` - `zone_id: string` Path param: Use this field to specify the unique ID of the zone. - `metadata: Metadata` Body param: Provide metadata about the snippet. - `main_module: string` Specify the name of the file that contains the main module of the snippet. ### Returns - `SnippetUpdateResponse` Contain the response result. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const snippet = await client.snippets.update('my_snippet', { zone_id: '9f1839b6152d298aca64c4e906b6d074', metadata: { main_module: 'main.js' }, }); console.log(snippet.created_on); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" }, "success": true } ``` ## Delete a zone snippet `client.snippets.delete(stringsnippetName, SnippetDeleteParamsparams, RequestOptionsoptions?): SnippetDeleteResponse | null` **delete** `/zones/{zone_id}/snippets/{snippet_name}` Deletes a snippet belonging to the zone. ### Parameters - `snippetName: string` Identify the snippet. - `params: SnippetDeleteParams` - `zone_id: string` Use this field to specify the unique ID of the zone. ### Returns - `SnippetDeleteResponse = unknown` Contain the response result. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const snippet = await client.snippets.delete('my_snippet', { zone_id: '9f1839b6152d298aca64c4e906b6d074', }); console.log(snippet); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Domain Types ### Snippet List Response - `SnippetListResponse` Define a snippet. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Snippet Get Response - `SnippetGetResponse` Contain the response result. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Snippet Update Response - `SnippetUpdateResponse` Contain the response result. - `created_on: string` Indicates when the snippet was created. - `snippet_name: string` Identify the snippet. - `modified_on?: string` Indicates when the snippet was last modified. ### Snippet Delete Response - `SnippetDeleteResponse = unknown` Contain the response result. # Content ## Get a zone snippet content `client.snippets.content.get(stringsnippetName, ContentGetParamsparams, RequestOptionsoptions?): Response` **get** `/zones/{zone_id}/snippets/{snippet_name}/content` Fetches the content of a snippet belonging to the zone. ### Parameters - `snippetName: string` Identify the snippet. - `params: ContentGetParams` - `zone_id: string` Use this field to specify the unique ID of the zone. ### Returns - `unnamed_schema_10 = Response` ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const content = await client.snippets.content.get('my_snippet', { zone_id: '9f1839b6152d298aca64c4e906b6d074', }); console.log(content); const data = await content.blob(); console.log(data); ``` # Rules ## List zone snippet rules `client.snippets.rules.list(RuleListParamsparams, RequestOptionsoptions?): RuleListResponse` **get** `/zones/{zone_id}/snippets/snippet_rules` Fetches all snippet rules belonging to the zone. ### Parameters - `params: RuleListParams` - `zone_id: string` Use this field to specify the unique ID of the zone. ### Returns - `RuleListResponse = unknown` Contain the response result. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const rules = await client.snippets.rules.list({ zone_id: '9f1839b6152d298aca64c4e906b6d074' }); console.log(rules); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Update zone snippet rules `client.snippets.rules.update(RuleUpdateParamsparams, RequestOptionsoptions?): RuleUpdateResponse` **put** `/zones/{zone_id}/snippets/snippet_rules` Updates all snippet rules belonging to the zone. ### Parameters - `params: RuleUpdateParams` - `zone_id: string` Path param: Use this field to specify the unique ID of the zone. - `rules: Array` Body param: Lists snippet rules. - `id: string` Specify the unique ID of the rule. - `expression: string` Define the expression that determines which traffic matches the rule. - `last_updated: string` Specify the timestamp of when the rule was last modified. - `snippet_name: string` Identify the snippet. - `description?: string` Provide an informative description of the rule. - `enabled?: boolean` Indicate whether to execute the rule. ### Returns - `RuleUpdateResponse = unknown` Contain the response result. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const rule = await client.snippets.rules.update({ zone_id: '9f1839b6152d298aca64c4e906b6d074', rules: [{ expression: 'ip.src eq 1.1.1.1', snippet_name: 'my_snippet' }], }); console.log(rule); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Delete zone snippet rules `client.snippets.rules.delete(RuleDeleteParamsparams, RequestOptionsoptions?): RuleDeleteResponse` **delete** `/zones/{zone_id}/snippets/snippet_rules` Deletes all snippet rules belonging to the zone. ### Parameters - `params: RuleDeleteParams` - `zone_id: string` Use this field to specify the unique ID of the zone. ### Returns - `RuleDeleteResponse = unknown` Contain the response result. ### Example ```node import Cloudflare from 'cloudflare'; const client = new Cloudflare({ apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted }); const rule = await client.snippets.rules.delete({ zone_id: '9f1839b6152d298aca64c4e906b6d074' }); console.log(rule); ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Domain Types ### Rule List Response - `RuleListResponse = unknown` Contain the response result. ### Rule Update Response - `RuleUpdateResponse = unknown` Contain the response result. ### Rule Delete Response - `RuleDeleteResponse = unknown` Contain the response result.