# Snippets ## List zone snippets `snippets.list(SnippetListParams**kwargs) -> SyncV4PagePaginationArray[SnippetListResponse]` **get** `/zones/{zone_id}/snippets` Fetches all snippets belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `page: Optional[int]` Specifies the current page number. - `per_page: Optional[int]` Specifies how many results to return per page. ### Returns - `class SnippetListResponse: …` Define a snippet. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) page = client.snippets.list( zone_id="9f1839b6152d298aca64c4e906b6d074", ) page = page.result[0] print(page.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 `snippets.get(strsnippet_name, SnippetGetParams**kwargs) -> SnippetGetResponse` **get** `/zones/{zone_id}/snippets/{snippet_name}` Fetches a snippet belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `snippet_name: str` Identify the snippet. ### Returns - `class SnippetGetResponse: …` Contain the response result. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) snippet = client.snippets.get( snippet_name="my_snippet", zone_id="9f1839b6152d298aca64c4e906b6d074", ) print(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 `snippets.update(strsnippet_name, SnippetUpdateParams**kwargs) -> SnippetUpdateResponse` **put** `/zones/{zone_id}/snippets/{snippet_name}` Creates or updates a snippet belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `snippet_name: str` Identify the snippet. - `metadata: Metadata` Provide metadata about the snippet. - `main_module: str` Specify the name of the file that contains the main module of the snippet. ### Returns - `class SnippetUpdateResponse: …` Contain the response result. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) snippet = client.snippets.update( snippet_name="my_snippet", zone_id="9f1839b6152d298aca64c4e906b6d074", metadata={ "main_module": "main.js" }, ) print(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 `snippets.delete(strsnippet_name, SnippetDeleteParams**kwargs) -> object` **delete** `/zones/{zone_id}/snippets/{snippet_name}` Deletes a snippet belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `snippet_name: str` Identify the snippet. ### Returns - `object` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) snippet = client.snippets.delete( snippet_name="my_snippet", zone_id="9f1839b6152d298aca64c4e906b6d074", ) print(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 - `class SnippetListResponse: …` Define a snippet. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. ### Snippet Get Response - `class SnippetGetResponse: …` Contain the response result. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. ### Snippet Update Response - `class SnippetUpdateResponse: …` Contain the response result. - `created_on: datetime` Indicates when the snippet was created. - `snippet_name: str` Identify the snippet. - `modified_on: Optional[datetime]` Indicates when the snippet was last modified. # Content ## Get a zone snippet content `snippets.content.get(strsnippet_name, ContentGetParams**kwargs) -> BinaryResponseContent` **get** `/zones/{zone_id}/snippets/{snippet_name}/content` Fetches the content of a snippet belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `snippet_name: str` Identify the snippet. ### Returns - `BinaryResponseContent` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) content = client.snippets.content.get( snippet_name="my_snippet", zone_id="9f1839b6152d298aca64c4e906b6d074", ) print(content) data = content.read() print(data) ``` # Rules ## List zone snippet rules `snippets.rules.list(RuleListParams**kwargs) -> object` **get** `/zones/{zone_id}/snippets/snippet_rules` Fetches all snippet rules belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. ### Returns - `object` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) rules = client.snippets.rules.list( zone_id="9f1839b6152d298aca64c4e906b6d074", ) print(rules) ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Update zone snippet rules `snippets.rules.update(RuleUpdateParams**kwargs) -> object` **put** `/zones/{zone_id}/snippets/snippet_rules` Updates all snippet rules belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. - `rules: Iterable[Rule]` Lists snippet rules. - `id: str` Specify the unique ID of the rule. - `expression: str` Define the expression that determines which traffic matches the rule. - `last_updated: Union[str, datetime]` Specify the timestamp of when the rule was last modified. - `snippet_name: str` Identify the snippet. - `description: Optional[str]` Provide an informative description of the rule. - `enabled: Optional[bool]` Indicate whether to execute the rule. ### Returns - `object` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) rule = client.snippets.rules.update( zone_id="9f1839b6152d298aca64c4e906b6d074", rules=[{ "expression": "ip.src eq 1.1.1.1", "snippet_name": "my_snippet", }], ) print(rule) ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Delete zone snippet rules `snippets.rules.delete(RuleDeleteParams**kwargs) -> object` **delete** `/zones/{zone_id}/snippets/snippet_rules` Deletes all snippet rules belonging to the zone. ### Parameters - `zone_id: str` Use this field to specify the unique ID of the zone. ### Returns - `object` ### Example ```python import os from cloudflare import Cloudflare client = Cloudflare( api_token=os.environ.get("CLOUDFLARE_API_TOKEN"), # This is the default and can be omitted ) rule = client.snippets.rules.delete( zone_id="9f1839b6152d298aca64c4e906b6d074", ) print(rule) ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ```